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:
And this is what I expected to get: