I am trying to upload a file to a generic HttpHandler in c#. I get the file using drag and drop events:
$s("#dropArea")
.on("drop", function(e) {
e.preventDefault && e.preventDefault();
var file = e.originalEvent.dataTransfer.files[0];
var reader = new FileReader();
reader.onload = function(evt) {
$s.ajax({
url: MY_SERVER_URL,
type: "POST",
data: reader.result,
headers: {
"x-file-name": file.name
}
});
};
reader.readAsDataURL(file);
this seems to work fine, but when posted to the ashx page, I try decode the image like so:
var form = context.Request.Form[0];
var attachment = form.Split(new[] {","}, StringSplitOptions.RemoveEmptyEntries)[1];
byte[] data = Convert.FromBase64String(attachment);
When I try to write byte[] data to the disk, the resulting file is not correct. Where am I going wrong?
edit: I have also tried using FormData, but the Request.Form and Request.Files collections are always empty and I have to parse the boundaries manually. This is the code I was using to do that:
var formData = new FormData();
formData.append(file.name, file);
$s.ajax({
url: MY_SERVER_URL,
type: "POST",
data: formData,
processData: false,
contentType: 'multipart/form-data',
mimeType: 'multipart/form-data',
headers: {
"x-file-name": file.name
});
formDataobject to upload the image ?false, and see if it works.falseto work with files.