HTTP 请求边界 JavaScript

发布于 2024-12-09 05:46:54 字数 2798 浏览 0 评论 0原文

我想知道如何处理边界内的二进制数据...现在我的代码正在提交原始数据,但我不确定如何包装它以供我的 C# 看到。

这是我当前的代码...

/*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();
        } 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");
};
};

我的 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>

和我的 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>

基本工作流程是:

“用户提交图像,上传图像时发生一些事情(更新进度条或其他内容),图像被发布到处理页面,页面将图像保存到自己的文件中,源页面显示已完成”

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();
        } 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"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

つ可否回来 2024-12-16 05:46:54

您没有发送 MIME 编码的文件,而只是发送文件内容,因此 HttpRequest.Files 对您来说毫无用处。使用 HttpRequest.InputStream 读取您发布的内容。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文