reportProgress Angular 不适用于 Web API AspNet?
reportProgress Angular doesn't work with Web API AspNet?
我正在编写任何文件的上传组件,我正在使用 angular 12 和 ASP.NET Web API 但报告进度不起作用。
我想做这样的事情:https://nemi-chand.github.io/multiple-file-upload-in-angular-using-aspnet-core/
uploadFile(files: File[]){
let formData = new FormData()
formData.append('file', files[0], files[0].name)
const req = new HttpRequest('POST', `${environment.serviceBaseApp}/UploadFile`, formData,
{
reportProgress: true
});
this.http.request(req).subscribe(event => {
switch (event.type) {
case HttpEventType.Sent:
console.log('Request sent!');
break;
case HttpEventType.ResponseHeader:
console.log('Response header received!');
break;
case HttpEventType.DownloadProgress:
const kbLoaded = Math.round(event.loaded / 1024);
console.log(`Download in progress! ${ kbLoaded }Kb loaded`);
break;
case HttpEventType.Response:
console.log(' Done!', event.body);
}
});
}
public class RecepcionController : ApiController
{
[HttpPost]
[Route("api/UploadFile")]
public HttpResponseMessage UploadFiles()
{
//Create the Directory.
string path = HttpContext.Current.Server.MapPath("~/Uploads/");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
//Save the File.
HttpPostedFile postedFile = HttpContext.Current.Request.Files[0];
string fileName = Path.GetFileName(postedFile.FileName);
postedFile.SaveAs(path + Path.GetFileName(postedFile.FileName));
//Send OK Response to Client.
return Request.CreateResponse(HttpStatusCode.OK, fileName);
}
}
而不是 DownloadProgress
你可能想要 UploadProgress
case HttpEventType.DownloadProgress: // this should be UploadProgress
const kbLoaded = Math.round(event.loaded / 1024);
console.log(`Download in progress! ${ kbLoaded }Kb loaded`);
break;
我正在编写任何文件的上传组件,我正在使用 angular 12 和 ASP.NET Web API 但报告进度不起作用。
我想做这样的事情:https://nemi-chand.github.io/multiple-file-upload-in-angular-using-aspnet-core/
uploadFile(files: File[]){
let formData = new FormData()
formData.append('file', files[0], files[0].name)
const req = new HttpRequest('POST', `${environment.serviceBaseApp}/UploadFile`, formData,
{
reportProgress: true
});
this.http.request(req).subscribe(event => {
switch (event.type) {
case HttpEventType.Sent:
console.log('Request sent!');
break;
case HttpEventType.ResponseHeader:
console.log('Response header received!');
break;
case HttpEventType.DownloadProgress:
const kbLoaded = Math.round(event.loaded / 1024);
console.log(`Download in progress! ${ kbLoaded }Kb loaded`);
break;
case HttpEventType.Response:
console.log(' Done!', event.body);
}
});
}
public class RecepcionController : ApiController
{
[HttpPost]
[Route("api/UploadFile")]
public HttpResponseMessage UploadFiles()
{
//Create the Directory.
string path = HttpContext.Current.Server.MapPath("~/Uploads/");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
//Save the File.
HttpPostedFile postedFile = HttpContext.Current.Request.Files[0];
string fileName = Path.GetFileName(postedFile.FileName);
postedFile.SaveAs(path + Path.GetFileName(postedFile.FileName));
//Send OK Response to Client.
return Request.CreateResponse(HttpStatusCode.OK, fileName);
}
}
而不是 DownloadProgress
你可能想要 UploadProgress
case HttpEventType.DownloadProgress: // this should be UploadProgress
const kbLoaded = Math.round(event.loaded / 1024);
console.log(`Download in progress! ${ kbLoaded }Kb loaded`);
break;