I have done this but did not use the HttpPostedFileBase
to get the content from the MVC app. I simply used JSON.
You can simply use the FileReader.onload
(on HTML5) method to extract the file content and post as a Base64 string directly to the MVC controller. The #upload-button
is an <input type=file ...>
tag.
var file = $('#upload-button')[0].files[0];
var reader = new FileReader();
reader.onload = (function (f) {
return function (e) {
if (e.target.readyState == FileReader.DONE) {
$.ajax("FileStore/SavePicture", {
data: { content: e.target.result, name: f.name },
type: "post",
contentType: "application/json"
});
}
};
})(file)
From there you can use Convert.FromBase64String
method to conver to a byte[]
. This is how the Action content look like;
string base64String = content;
// Get the starting point of the actual content from the base64String.
int start = base64String.IndexOf(",") + 1;
int length = base64String.Length - start;
contentAsString = base64String.Substring(start, length);
byte[] dataAsBytes = Convert.FromBase64String(contentAsString);
There might be some other ways of doing this.