作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试在 asp.net core 2.0 中使用 angular 5 上传文件。
这是我的服务器端代码。
public class QuestionViewModel
{
public Guid QuestionId { get; set; }
public string QuestionText { get; set; }
public DateTime CreateDate { get; set; }
public string PictureUrl { get; set; }
public FormFile FileUpload { get; set; }
}
这里是 Controller 方法。
[HttpPost]
[AllowAnonymous]
public JsonResult QuestionPhotoPost([FromBody] QuestionViewModel model)
{
GenericResponseObject<List<QuestionViewModel>> genericResponseObject = new GenericResponseObject<List<QuestionViewModel>>();
genericResponseObject.IsSuccess = false;
genericResponseObject.Message = ConstaintStingValue.TagConnectionFailed;
List<QuestionViewModel> questionViewModel = new List<QuestionViewModel>();
return Json(genericResponseObject);
}
类型脚本类
export class Data {
QuestionText: string = "";
FileUpload: File;
}
这是 http 调用。调用是对 Controller 方法的调用。
public QuestionPostHttpCall(_loginVM: QuestionVmData): Observable<QuestionPhotoViewModel> {
console.log(_loginVM)
const headers = new HttpHeaders().set('Content-Type', 'application/json; charset=utf-8');
return this._httpClientModule.post<QuestionPhotoViewModel>(this.questionPhoto, _loginVM, { headers});
}
这是发送到服务器之前的数据。
但在 Controller 中文件的值为空。
其他属性绑定(bind)到 Controller 参数只有文件没有绑定(bind)。
谁能告诉我我哪里做错了。 引用资料 - ASP.NET Core 2.0 and Angular 4.3 File Upload with progress
最佳答案
您需要发送像 multipart/form-data 这样的文件。
upload(file: File, questionText: string): Observable<FileResponseModel> {
const url: string = Urls.uploadFiles();
const formData: FormData = new FormData();
formData.append('attachment', file, file.name);
formData.append('questionText', questionText);
const options = {
headers: new HttpHeaders().set('enctype', 'multipart/form-data')
};
return this.httpService.post(url, formData, options);
}
关于asp.net - 在 asp.net core 2.0 中使用 angular 5 上传文件。文件为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48731744/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!