簡體   English   中英

無法將文件從Angular 8上傳到Asp.net Core 2.2

[英]Unable to File Upload From Angular 8 to Asp.net Core 2.2

我有一個具有FileUploadController的 asp.net核心服務器(使用.net core 2.2),該服務器監聽發布文件的請求。

[HttpPost("Upload")]
// public async Task<IActionResult> Upload([FromForm(Name="file")]IFormFile file) {
// public async Task<IActionResult> Upload([FromForm]IFormFile file) {
public async Task<IActionResult> Upload(IFormFile file) {
   Console.WriteLine("***" + file);
   if(file == null) return BadRequest("NULL FILE");
   if(file.Length == 0) return BadRequest("Empty File");
       Console.WriteLine("***" + host.WebRootPath);
   if (string.IsNullOrWhiteSpace(host.WebRootPath))
   {
      host.WebRootPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot");
   }
   var uploadsFolderPath = Path.Combine(host.WebRootPath, "uploads");
   if (!Directory.Exists(uploadsFolderPath)) Directory.CreateDirectory(uploadsFolderPath);
       var fileName = "Master" + Path.GetExtension(file.FileName);
       var filePath = Path.Combine(uploadsFolderPath, fileName);
       using (var stream = new FileStream(filePath, FileMode.Create))
       {
          await file.CopyToAsync(stream);
       }
       return Ok("Okay");
}  

我已經創建了角度應用程序(使用角度版本8),該應用程序可以選擇要在ClientApplication上載的文件,並且創建了三個后服務,它們調用了api“ http:// localhost:5000 / api / fileupload / upload ”。

  1. 標准Angular HttpClient發布。 服務器讀取時,IFormFile為null。

     const formData: FormData = new FormData(); formData.append('file', file, file.name); // return this.http.post(this.endpoint, file); return this.http.post(this.endpoint, formData); // Problem solved 

400錯誤的請求NULL文件

  1. 添加了HttpHeaders,我嘗試使用空標題,未定義標題以及其他來自stackoverflow和google的提議解決方案。

     const header = new HttpHeaders() //1 header.append('enctype', 'multipart/form-data'); //2 header.append('Content-Type', 'multipart/form-data'); //3 

如果我將帶有資源的httpheader放入請求中,則服務器會給出415(不受支持的媒體類型)

415錯誤的請求不支持的媒體類型

  1. 我嘗試來自“ @ angular / common / http”的HttpRequest,最終給了我想要的結果。

     const formData: FormData = new FormData(); formData.append('file', file, file.name); const req = new HttpRequest('POST', this.endpoint, formData); return this.http.request(req); 

我想知道這是錯誤還是我的誤解? 如果您查看在線教程,則大多數開發人員都使用“ this.HttpClient.post”。 從我閱讀的內容中,我可以使用httpclient.post,Angular框架將自動為用戶設置適當的標頭。 似乎沒有完成這項工作。

經過深入研究,第一個錯誤是我使用文件而不是formData的錯誤,第二個錯誤是在httpinterceptor中聲明的標頭“ content-type”,該標頭在刪除后按預期加載了文件。

@Injectable()
export class JwtInterceptor implements HttpInterceptor {
    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        // add authorization header with jwt token if available
        // if (request.url.indexOf('/upload')) {
        //     return next.handle(request);
        // }
        const token = localStorage.getItem('token');
        const currentUser = JSON.parse(localStorage.getItem('user'));
        if (currentUser && token) {
            request = request.clone({
                setHeaders: {
                    Authorization: `Bearer ${token}`,
                //    'Content-Type': 'application/json' <---- Main Problem.
                }
            });
        }
        return next.handle(request).pipe(catchError(err => this.handleError(err)));
    }
}

服務器:“ https://github.com/phonemyatt/TestPlaygroundServer

客戶端:“ https://github.com/phonemyatt/TestPlayground

下面的代碼為您工作

  uploadSecond(file: File) {
    const formData: FormData = new FormData();
    formData.append('file', file, file.name);
    return this.http.post('https://localhost:44393/api/fileupload/UploadSecond', formData);
  }

然后在您的控制器中

[HttpPost("UploadSecond")]
[DisableRequestSizeLimit]
public async Task<IActionResult> UploadSecond([FromForm]IFormFile file)

在第一個無效的示例中,您將file傳遞到post(...)而不是formData 它應該是:

const formData: FormData = new FormData();
formData.append('file', file, file.name);
return this.http.post(this.endpoint, formData);

您為控制器顯示的代碼似乎正確,因此這應該是唯一需要的更改。 並不需要設置對從角發送請求的任何自定義頁眉。

如果在客戶端中使用FormData,則可以獲取這樣的文件。

[HttpPost("Upload"), DisableRequestSizeLimit]
        public ActionResult Upload()
        {
            try
            {
                var file = Request.Form.Files[0];
                var folderName = Path.Combine("Resources","Images");
                var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), folderName);

                if (file.Length > 0)
                {
                    var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                    var fullPath = Path.Combine(pathToSave, fileName);
                    var dbPath = Path.Combine(folderName, fileName);

                    using (var stream = new FileStream(fullPath, FileMode.Create))
                    {
                        file.CopyTo(stream);
                    }

                    return Ok(new { dbPath });
                }
                else
                {
                    return BadRequest();
                }
            }
            catch (Exception ex)
            {
                return StatusCode(500, "Internal server error");
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM