1

I implemented in my API a Controller to upload files with an HTTP Postrequest, nothing challenging from what you find on the web:

public async Task<IActionResult> Post()
{
    if (string.IsNullOrEmpty(Request.GetMultipartBoundary()))
    {
        return StatusCode(415);
    }

    long size = Request.Form.Files.Sum(f => f.Length);

    var filePath =
        Path.Combine(_hostingEnvironment.ContentRootPath, "TempUploads");

    string fileName = "";

    List<string> tempFileName = new List<string>();

    foreach (var formFile in Request.Form.Files)
    {
        fileName = formFile.FileName;
        string timeStamp = DateTime.Now.ToString("yyyyMMddHHmmssfff");
        fileName = fileName.Replace(Path.GetExtension(formFile.FileName),
            timeStamp + Path.GetExtension(formFile.FileName));

        filePath = filePath + "\\" + fileName;
        if (formFile.Length > 0)
        {
            using(var stream = new FileStream(filePath, FileMode.Create))
            {
                await formFile.CopyToAsync(stream);
            }
        }

        tempFileName.Add(fileName);
    }

    return Ok(new { tempFileName });
}

It adds a timeStamp to the name, but I already tried without it and it doesn't change anything.

The problem I get is that if I upload files bigger than around 4KB (still really small) they either are corrupted (won't open) or are empty (same amount of pages, but all blank) and weight a couple KB more.

Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
Rep
  • 11
  • 1
  • 1
    Have you taking a look at https://dotnetcoretutorials.com/2017/03/12/uploading-files-asp-net-core/? It has a pretty good walkthrough. – Marc LaFleur Mar 21 '18 at 19:24
  • I'm not really in a situation where I need to stream, and the buffering version is not very complete, plus I've searched everywhere and they all implement it the way I did but can upload large files. – Rep Mar 21 '18 at 19:29
  • I'll delete my answer as it is patently wrong. Sorry @Rep – sheavens Mar 21 '18 at 19:30
  • No problem, thanks for your time – Rep Mar 21 '18 at 19:31
  • You could try declaring your filename inside the foreach block as I think the filename will change while still processing when there is more than one file being uploaded – sheavens Mar 21 '18 at 19:45
  • I'm wondering if there is an issue with your function prototype. I went through a couple of projects I have with file uploads and noticed all of them have a parameter `List files`. You may want to try `public async Task Post(List files)` rather than pulling them from the Request object. – Marc LaFleur Mar 21 '18 at 19:49
  • Yeah, most examples do it like that, I didn't at first because for some reason using PostMan it doesn't work getting the file like that, however in one of all the tests I did I tried creating a view for the api to do the request and I implemented it like you suggested and got the same result – Rep Mar 21 '18 at 20:06
  • Is it possible the problem is external to the controller (client, etc)? Have you tried a non-async version like https://stackoverflow.com/a/15680783/1237135 to see if that works OK? – Jim W Mar 21 '18 at 20:15
  • I just tried non async and checked my view as the link suggested, but nothing changed. I doubt it is how i get the file the problem – Rep Mar 21 '18 at 20:26
  • How big are the files you're trying to upload? And are you using IIS for hosting? – Jim W Mar 21 '18 at 20:31
  • I have one that is 3KB and uploads perfectly fine, then I have one of 8KB which doesn't.. – Rep Mar 21 '18 at 20:33
  • The only thing I can think of besides the client not sending it properly is https://stackoverflow.com/questions/38698350/increase-upload-file-size-in-asp-net-core but by default that shouldn't be the issue for an 8kb file – Jim W Mar 21 '18 at 21:12

1 Answers1

0

Just found out what was the problem in my particular case, I was using a Middleware for logging and request handling and that for some reason affects the request, if I disable the Middleware it uploads perfectly.

Rep
  • 11
  • 1