1

I am uploading multiple files asynchronously using ASP.NET Core. As soon as the user clicks browse and selects multiple files the request is sent to the server. They are being processed simultaneously so debugging seems very strange.

I would like to allow the user to upload multiple files at once, but process them in order in the following way:

File1: Save to filesystem and store info about file in database.
File2: When everything from File1 is completely finished, Save to filesystem and store info about the file in database.
File3: When everything from File2 is completely finished, Save to filesystem and store info about the file in database.

The problem that I am facing is that File2 database info relies on data from File1, File3 database info relies on data from File2... I want to make a call to the database to get the previous file to do this but I can't because it never exists yet due to the way everything is processing.

There is a setting for the uploader control I am using called: SequentialUpload. This setting seems like it should help with exactly what I want based on the description, but in the debugger it still looks strange and I am unable to process in order.

When you enable the sequentialUpload property, the selected files will process sequentially (one after the other) to the server. If the file uploaded successfully or failed, the next file will upload automatically in this sequential upload.

Below is what my server-side function looks like, excluding the saving info to the database since that is business logic:

// Upload method for chunk-upload and normal upload
public IActionResult Save(IList<IFormFile> UploadFiles)
{
    long size = 0;
    // for normal upload
    try
    {
        foreach (var file in UploadFiles)
        {
            var filename = ContentDispositionHeaderValue
                            .Parse(file.ContentDisposition)
                            .FileName
                            .Trim('"');
            filename = hostingEnv.WebRootPath + $@"\{filename}";
            size += file.Length;
            if (!System.IO.File.Exists(filename))
            {
                using (FileStream fs = System.IO.File.Create(filename))
                {
                    file.CopyTo(fs);
                    fs.Flush();
                }

                // LOGIC TO RETRIEVE INFO ABOUT PREVIOUSLY STORED FILE FROM DB HERE
                // LOGIC TO STORE INFO ABOUT CURRENT FILE TO DB HERE
            }
        }
    }
    catch (Exception e)
    {
        Response.Clear();
        Response.StatusCode = 204;
        Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "File failed to upload";
        Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = e.Message;
    }
    return Content("");
}

It would be perfect if File1 is processed then I end up in the success function on the client and then File2 is kicked off and so on... Instead I always end up in the success function after all processing is done.

Blake Rivell
  • 13,105
  • 31
  • 115
  • 231

2 Answers2

0

It seems that you're sending all files in one request, so you'll have only one return.

If I get it right, you need to send a file per request, so you'll have a success call for each file, then you'll be able sync the kick for file in cascade using the callbacks.

Luchini
  • 75
  • 1
  • 7
  • I started thinking about this and I think rather than sending them all at once, I should simply do a standard AJAX call posting the form data. Just like you would manually post a form using AJAX with .net core. Does this make sense? I am having trouble figuring out how to send the files because they are not being bound to my IList on the server end. – Blake Rivell Jan 26 '19 at 20:08
  • I normally get the file from context, "_context.Request.Files_". [upload file using jquery](https://stackoverflow.com/questions/8466941/upload-file-using-jquery-and-handlerashx) – Luchini Jan 26 '19 at 20:15
0

When using sequential upload API, the selected files will be sent to the server one by one. After the first file gets processed, whether it may success or failure, the second one starts to upload. You can verify it in this sample.

Also, ensure that you are using the Syncfusion ej2 packages version 16.4.40 or above. If you are still facing the same issue, please revert a sample or code sufficient to replicate the issue. This will help to provide a solution.

Prince Oliver
  • 241
  • 1
  • 4