我在创建教程和同时上传文件时遇到问题。每次我尝试,邮递员都会告诉我:
“错误:错误请求”
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "|6030eedc-42a22f9f9c01d926.",
"errors": {
"$.file": [
"The JSON value could not be converted to Microsoft.AspNetCore.Http.IFormFile. Path: $.file | LineNumber: 0 | BytePositionInLine: 141."
]
}
}
我不知道如何解决这个问题...
在我的 TutorialDTO.cs 我有
public class TutorialDTO
{
public string Title { get; set; }
public string PathToMdFile { get; set; }
public string Description { get; set; }
public string VideoLink { get; set; }
public IList<CommentDTO> Comments { get; set; }
public IFormFile file { get; set; }
}
在我的 TutorialController.cs 我有
[HttpPost]
[DisableRequestSizeLimit]
public async Task<Tutorial> PostTutorial(TutorialDTO tutorial)
{
if (tutorial.file != null)
{
try
{
//var file = Convert.ToBase64String(Encoding.Unicode.GetBytes(tutorial.file));
string path_to_Files = Configuration.GetConnectionString("PathToFiles") + tutorial.file.FileName;
using (var stream = new FileStream(path_to_Files, FileMode.Create))
{
//file = new IFormFile("") { };
await tutorial.file.CopyToAsync(stream);
}
Tutorial tutorialToCreate = new Tutorial() { Title = tutorial.Title, Description = tutorial.Description, VideoLink = tutorial.VideoLink, PathToMdFile = path_to_Files };
_dbContext.Tutorials.Add(tutorialToCreate);
_dbContext.SaveChanges();
return tutorialToCreate;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return null;
}
else
{
Tutorial tutorialToCreate = new Tutorial() { Title = tutorial.Title, Description = tutorial.Description, VideoLink = tutorial.VideoLink, PathToMdFile = tutorial.PathToMdFile };
_dbContext.Tutorials.Add(tutorialToCreate);
_dbContext.SaveChanges();
return tutorialToCreate;
}
}
有人可以帮助我吗?这几天我一直在为这个问题苦苦挣扎。而且我开始厌倦这个...