Home > Mobile >  Asp.net core upload file size is wrong
Asp.net core upload file size is wrong

Time:08-05

I wrote a file upload function with asp.net Core and I call this function with Angular httpclient But the problem is that the file size on the server side is bigger and when it is saved, the file gets corrupted

For example, an image file with a size of 43 kilobytes becomes 79 kilobytes on the server side

It can be said that the size of the files is doubled

And that files larger than 30 MB do not have this problem

api

[Authorize]
    [HttpPost("[action]") ]
    [RequestFormLimits(MultipartBodyLengthLimit = 309715200)]
    [RequestSizeLimit(309715200)]
    public async Task<IActionResult> Upload(IFormFile file )
    {
        try
        {       
            if (file.Length > 0)
            {
                
                using (var ms = new MemoryStream())
                {
                    file.CopyTo(ms);
                    var fileBytes = ms.ToArray();
                   var filelist =  _fileManager.SaveFile(file.FileName,fileBytes);
                    if (filelist == null)
                        return BadRequest();
                    return Ok(new {
                        nofile = filelist.StreamId
                });                  
                }
            }
            else
            {
                return BadRequest();
            }
        }
        catch (Exception ex)
        {
            return StatusCode(500, "Internal server error");
        }
    }

angular

UploadFile(file:any ): Observable<any>
    {
        
        const formData: FormData = new FormData();
        formData.append('file', file);
        let headers = new HttpHeaders();
            headers = headers.set(
                'Content-Type',
                 'multipart/form-data;'
            );

        return    this._httpClient
            .post(this.uploadUrl,formData,{ reportProgress: true, observe: 'events' }
            )

    }

CodePudding user response:

My problem is solved I had a middleware that replaced a series of characters, and disabling it solved the problem

  • Related