I'm wondering how to handle binary data in boundaries... Right now my code is submitting raw data, but I'm not sure how to wrap it to be seen by my C#.
Here's my current code...
/*global window, document, XMLHttpRequest, FormData, FileReader, Uint8Array, alert, BlobBuilder, navigator */
function sendForm(filefield) {
"use strict";
var reader, filedata, bb, xhr, blobber;
filedata = document.getElementById(filefield).files[0];
reader = new FileReader();
reader.onloadend = function (evt) {
if (evt.readyState === FileReader.Done) {
if (navigator.userAgent.indexOf("Chrom") !== -1) {
bb = new window.WebKitBlobBuilder();开发者_如何学C
} else if (navigator.userAgent.indexOf("Firefox") !== -1) {
bb = new window.MozBlobBuilder();
} else {
bb = new BlobBuilder();
}
bb.append(reader.result);
blobber = bb.getBlob();
xhr = new XMLHttpRequest();
xhr.open("POST", "imageprocessor.aspx", true);
xhr.setRequestHeader("Content-type", "image/png");
xhr.setRequestHeader("Content-length", reader.length);
xhr.send(blobber);
}
};
reader.readAsArrayBuffer(filedata);
}
window.onload = function () {
"use strict";
document.getElementById("sender").onclick = function () {
sendForm("file");
};
};
My html...
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<title>Test</title>
<script src="fileuploader.js" type="text/javascript"></script>
</head>
<body>
<div>
<input type="file" name="file" id="file" />
</div>
<div><input type="button" value="送る" id="sender" /></div>
</body>
</html>
And my C#...
<%@ Page language="C#" validateRequest=false %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Threading" %>
<script language="C#" runat="server">
private void Page_Load (object sender, System.EventArgs e)
{
int loop1;
HttpFileCollection Files;
Files = Request.Files; // Load File collection into HttpFileCollection variable.
Response.Write(Files.Count);
// arr1 = Files.AllKeys; // This will get names of all files into a string array.
// for (loop1 = 0; loop1 < arr1.Length; loop1++)
// {
// Response.Write("File: " + Server.HtmlEncode(arr1[loop1]) + "<br />");
// Response.Write(" size = " + Files[loop1].ContentLength + "<br />");
// Response.Write(" content type = " + Files[loop1].ContentType + "<br />");
// }
}
</script>
The basic workflow is:
"User submits image, stuff happens while image is being uploaded (updating progress bar or something), image is posted to processing page, page saves image to its own file, source page displays that it's done"
You are not sending a MIME encoded file, you just sending file content, so HttpRequest.Files
will be useless to you. Use the HttpRequest.InputStream
to read the stuff you posted.
精彩评论