Pregunta

I am newbie to web development and I have a have a problem when saving a file to database. I am using MVC 4, knockoutjs 2.3.0, jquery 2.03 and amplifyjs 1.1.0.

Here is how I want it to be done.

In my upload page I have a form:

<form id="uploadForm" name="uploadForm" action="~/Administration/UploadReports"       method="post" enctype="multipart/form-data">
    <input required type="file" name="fileToUpload" id="fileToUpload" accept="html, image/jpeg">
        <input type="submit" data-bind="click:submit" value="Ladda upp fil" />
        <input type="reset" value="Avbryt" />
    </form>

Then in my knockoutViewModel I have the code below:

 this.submit = function (model, element) {
                var test = $('#uploadForm')[0];
                var formData = new FormData(test);

                amplify.request({
                    resourceId: "uploadReport",
                    success: () => {
                        console.log("success");
                    },
                    error: () => {
                        console.log("error");
                    },
                    data: formData 
                });                   
            };

So far every thing works fine. I end up here when I press the button in my form. But I think amplifyjs is doing something with my dataForm... Amplify.request is define as below:

amplify.request.define('uploadReport', 'AJAX', {
    url: '/Administration/UploadReports',
    type: 'POST',
    contentType: false,
    processData: false,
    cache: false
});

And in my controller it looks like this:

    [HttpPost]
    public ActionResult UploadReports(HttpPostedFileBase fileToUpload)
    {

        //Update the list of files
        var model = new AdministrationViewModel();

        byte[] result;

        using (var streamReader = new MemoryStream())
        {
            fileToUpload.InputStream.CopyTo(streamReader);
            result = streamReader.ToArray();
        }

        model.BetFiles = FileInserter.InsertFile(fileToUpload.FileName, fileToUpload.ContentType, fileToUpload.ContentLength, result).Value;


        return View("Reports", model);
    }

Can anyone help me to see what is the problem?

Regards, David


Here is what I get:

enter image description here

And this is what I expected to get:

enter image description here

¿Fue útil?

Solución 2

If I change the this.submit function in my knockout viewmodel to the code below it works as it should but i want to use amplify to get this.

 this.submit = function (model, element) {
                var test = $('#uploadForm')[0];

                var formURL = formObj.attr("action");
                var formData = new FormData(test);

                $.ajax({
                    url: formURL,
                    type: 'POST',
                    data: formData,
                    mimeType: "multipart/form-data",
                    contentType: false,
                    cache: false,
                    processData: false,
                    success: function (data, textStatus, jqXHR) {

                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                    }
                });

            };

and of course in my controller method I should return:

return Json(model.BetFiles);

Instead of a View.

Otros consejos

Currently amplify.js does not support FormData in version 1.1.2.

Check this issue https://github.com/appendto/amplify/issues/107

The possible solution is modify the library in this event subscription

amplify.subscribe( "request.ajax.preprocess", function(defnSettings, settings, ajaxSettings) {
        var mappedKeys = [],
            data = ajaxSettings.data;

        if (typeof data === "string" || Object.prototype.toString.call(data) === "[object FormData]") {
            return;
        }
......(omited)
)}

That way amplify will ignore the object passed if is a native FormData object. The settings in the request definition must include to avoid jQuery messing with your FormData object again

contentType: false
cache: false
processData: false
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top