我试图通过将文件封装在formdata中并调用asp.net webmethod来按角度上传文件。 HttpContext.Current.Request.Files.Count始终返回0。表单数据未发布的任何原因。我做错什么了吗?在调用方法之前,我可以看到formdata已正确地构建在angular中
组件
import { NgxFileDropEntry, FileSystemFileEntry, FileSystemDirectoryEntry } from 'ngx-file-drop';
export interface IDocumentUpload {
fileDropEntry: NgxFileDropEntry;
name: string;
selectedDocumentItem: { 'Id': number; 'Name': string; }
selectedDate: Date;
}
public files: IDocumentUpload[] = [];
public uploadDocument(ids: number[]) {
const formData = new FormData();
let inx = 1;
this.files.forEach(x => {
const fileEntry = x.fileDropEntry.fileEntry as FileSystemFileEntry;
fileEntry.file((file: File) => {
formData.append('file' + inx++, file, x.name);
});
this.documentUploadService.UploadDocument(formData).then(data => {
this.getDocumentUploadDetailsByIds(this.documentIds.ids.toString());
this.setGridOptions();
this.setColumns();
this.notify.success('Documents uploaded Successfully');
this.upload = true;
})
});
}
UploadDocument(formData: any) {
return this.mgr360CommonService.httpPostAsync('/api/documentupload/uploadDocument',formData);
}
httpPostAsync(url: string, model: any) {
return this.httpClient.post(this.webApiLocation + url, model, httpPostOptions)
.pipe(map((response: Response) => {
return response;
}))
.toPromise()
.catch((error: any) => {
this.onError(error);
return Promise.reject(error);
});
}
asp.net webap
[HttpPost]
[SkipTokenAuthorization]
[Route("api/documentupload/uploadDocument")]
public IHttpActionResult uploadDocument()
{
HttpResponseMessage response;
var mgrDocumentService = GetService<DOCUMENT>();
if (HttpContext.Current.Request.Files.Count == 0)
return null;
var attachmentIds = new List<int>();
foreach (string fileName in HttpContext.Current.Request.Files)
{
var file = HttpContext.Current.Request.Files[fileName];
if (file != null && file.ContentLength > 0)
{
Stream fs = file.InputStream;
BinaryReader br = new BinaryReader(fs);
byte[] bytes = br.ReadBytes((Int32)fs.Length);
}
else
{
throw new Exception("The file is missing.");
}
//return response = Request.CreateResponse(HttpStatusCode.OK, mgrStrategyDocument);
}
return null;
}
console.log(formData)的屏幕截图