0

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' }
            )

    }

2
  • 1
    I created a sample angular app and couldn't reproduce the issue. Also headers isn't used by your http post so deleted my answer. I would check how you're verifying the file length and whether you have a proxy / middleware setup that could be interfering.
    – Shoejep
    Commented Aug 2, 2022 at 6:54
  • My problem is solved I had a middleware that replaced a series of characters, and disabling it solved the problem Commented Aug 2, 2022 at 7:02

1 Answer 1

1

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

1
  • Thank you for the cool point you mentioned Commented Apr 19, 2024 at 6:39

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.