user3620475
user3620475

Reputation: 13

Fineuploader dotnet core

I'm trying to get write Controller (in asp.net core) code to handle my fineuploader requests but it keeps failing to see the request data despite whatever combination of [FromForm] / [FromBody] / "Content-Type": 'application/json' I use. Here is my uploader config in the view page:

var uploader = new qq.FineUploader({
        element: document.getElementById("uploader"),
        request: {
            endpoint: '@Url.Action("AddAttachment", "Util")',
            customHeaders: {
                "RequestVerificationToken": '@GetAntiXsrfRequestToken()',
                "Content-Type": 'application/json'
            }
        },

And here is my controller code - not alot for now, I just literally want to see some data getting sent.

public JsonResult AddAttachment([FromForm] Object o){
         //var x = HttpContext.Request;
         //return Json(x);
         if(o == null){
             return Json("no data");
         }else{
             return Json(o);
         }
    }

and her is what I see fineuploader sending to the server via the network tab in the chrome devtools:

------WebKitFormBoundarySQwYoYovQOkoFU1f
Content-Disposition: form-data; name="test"

876
------WebKitFormBoundarySQwYoYovQOkoFU1f
Content-Disposition: form-data; name="qquuid"

9ba04b80-b3d8-4e2d-8068-792dd77253bd
------WebKitFormBoundarySQwYoYovQOkoFU1f
Content-Disposition: form-data; name="qqfilename"

dohPlayDat.PNG
------WebKitFormBoundarySQwYoYovQOkoFU1f
Content-Disposition: form-data; name="qqtotalfilesize"

3535659
------WebKitFormBoundarySQwYoYovQOkoFU1f
Content-Disposition: form-data; name="qqfile"; filename="dohPlayDat.PNG"
Content-Type: image/png


------WebKitFormBoundarySQwYoYovQOkoFU1f-

Can anyone see the mistake I'm making?

Upvotes: 0

Views: 915

Answers (2)

Nathan
Nathan

Reputation: 663

The following snippet is an example of how you can bind all the fields from the file upload request, then use the file stream to write the file to a temporary file.

public async Task<IActionResult> Post(string qquuid, string qqfilename, int qqtotalfilesize, IFormFile qqfile)
        {
            // full path to file in temp location
            var filePath = Path.GetTempFileName();

            if (qqfile.Length > 0)
            {
                using (var stream = new FileStream(filePath, FileMode.Create))
                {
                    await qqfile.CopyToAsync(stream);
                }
            }
        }

For more information about using .NET Core to upload files you can have a look over here : https://learn.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads?view=aspnetcore-2.1

Upvotes: 0

Mithgroth
Mithgroth

Reputation: 1212

If everything's wired up correctly, you should be able to see your file via Request.Form.Files in the controller.

I was trying to get it as a byte array by action's parameters but had no luck. Try this instead.

Upvotes: 1

Related Questions