gpt4 book ai didi

c# - 如何将数据从网络表单页面发布到 HTTPHandler.ashx 文件?

转载 作者:行者123 更新时间:2023-11-30 13:05:57 25 4
gpt4 key购买 nike

我有一个 Web 应用程序项目来支持到供应商产品后端的文件传输操作。它由 2 个 HTTPHandler 文件组成,这些文件在装有 IIS 6.0 的 Win2003 服务器上编译成一个网站:

  • 上传处理程序.ashx
  • 下载Handler.ashx

这些文件从公开用户界面的 ColdFusion 网站“发布到”。在某种程度上,我的工作已经完成,因为这些处理程序可以工作并且必须从 ColdFusion 调用。

但是,我对自己无法独立于 ColdFusion 进行测试/改进而无法使用自己的“测试 UI”(default.aspx) 感到非常沮丧。

<asp:Button ID="DownloadButton" PostBackUrl="~/DownloadHandler.ashx"  runat="server" Text="Download"/>

使用 PostBackUrl 进行下载效果很好 - 当输入 DownloadHandler.ashx 时,它会在 context.Request.Form["txtRecordNumber"];

中找到其关键输入值

但我不能将此技术用于上传,因为我必须进行一些处理(以某种方式将所选文件 upload1.postedfile 中的字节读取到 FORM 变量中,以便我的 UploadHandler.ashx 文件可以从Request.Form 与下载一样)

My first approach tried using HTTPWebRequest这似乎过于复杂,我永远无法开始工作。症状以 HTTP 401 状态代码开始,然后演变为 302 状态代码,因此我研究了其他想法。

这是我的 default.aspx 中的最新代码片段:

protected void UploadHandlerButton_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
try
{
BuildFormData();
//Server.Transfer("UploadHandler.ashx", true);
Response.Redirect("~/UploadHandler.ashx");
}
catch (Exception someError)
{
LogText("FAILURE: " + someError.Message);
}
}
}
protected void BuildFormData()
{
BinaryReader b = new BinaryReader(FileUpload1.PostedFile.InputStream);
int numBytes = FileUpload1.PostedFile.ContentLength;
byte[] fileContent = b.ReadBytes(numBytes);
objBinaryData.Text = System.Text.Encoding.UTF8.GetString(fileContent);
b64fileName.Text = FileUpload1.PostedFile.FileName;
// create arbitrary MetaData in a string
strMetaData.Text = "recAuthorLoc=anyname1~udf:OPEAnalyst=anyname2~udf:Grant Number=0102030405";
}

尝试对我的 .ashx 文件使用 Server.Transfer(上文)会导致错误: 执行 UploadHandler.ashx 的子请求时出错

尝试对我的 .ashx 文件使用 Response.Redirect(上图)导致 GET(不是 POST)并且 Trace.axd 当然在 Form 集合中没有显示任何内容,所以这似乎也是错误的。

我什至尝试克隆我的 .ashx 文件并创建 UploadPage.aspx(一个没有 HTML 元素的网络表单),然后尝试:

Server.Transfer("UploadPage.aspx", true);
//Response.Redirect("~/UploadPage.aspx");

这些都不允许我在处理上传请求的代码中看到我需要在 Request.Form 中看到的表单数据。我显然在这里遗漏了一些东西......在此先感谢您的帮助。

编辑更新:我想我可以澄清我的问题。当 UploadHandler.ashx 从 ColdFusion 发布时,它需要的所有输入都在 FORM 集合中可用(例如 Request.Form["fileData"] 等)

但是当我使用这个 控件时,它会生成一个回发到我的启动网页(即 default.aspx)。这使我能够通过 FileUpload1.PostedFile 引用内容,如:

protected void BuildFormData()
{
BinaryReader b = new BinaryReader(FileUpload1.PostedFile.InputStream);
int numBytes = FileUpload1.PostedFile.ContentLength;
byte[] fileContent = b.ReadBytes(numBytes);
objBinaryData.Text = System.Text.Encoding.UTF8.GetString(fileContent);
b64fileName.Text = FileUpload1.PostedFile.FileName;
}

但我没有使用 FileUpload1.PostedFile.SaveAs 方法将文件保存在我的网络服务器上的某个位置。我需要以某种方式 - 请原谅这里的语言 - “重新发布” 此数据到一个完全不同的文件 - 即我的 UploadHandler.ashx 处理程序。我在上面尝试过的所有愚蠢的技术都无法完成我需要的。

EDIT-UPDATE(2009 年 8 月 20 日)- 我使用 Javascript 的最终解决方案:

protected void UploadHandlerButton_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
try
{
ctlForm.Text = BuildFormData();
String strJS = InjectJS("_xclick");
ctlPostScript.Text = strJS;
}
catch (Exception someError)
{
LogText("FAILURE: " + someError.Message);
}
}
}
private String InjectJS(String strFormId)
{
StringBuilder strScript = new StringBuilder();
strScript.Append("<script language='javascript'>");
strScript.Append("var ctlForm1 = document.forms.namedItem('{0}');");
strScript.Append("ctlForm1.submit();");
strScript.Append("</script>");
return String.Format(strScript.ToString(), strFormId);
}
protected string BuildFormData()
{
BinaryReader b = new BinaryReader(FileUpload1.PostedFile.InputStream);
int numBytes = FileUpload1.PostedFile.ContentLength;
byte[] fileContent = b.ReadBytes(numBytes);
// Convert the binary input into Base64 UUEncoded output.
string base64String;
base64String =
System.Convert.ToBase64String(fileContent,
0,
fileContent.Length);
objBinaryData.Text = base64String;
b64fileName.Text = FileUpload1.PostedFile.FileName;
// create arbitrary MetaData in a string
strMetaData.Text = "recAuthorLoc=Patterson, Fred~udf:OPEAnalyst=Tiger Woods~udf:Grant Number=0102030405";

StringBuilder strForm = new StringBuilder();
strForm.Append("<form id=\"_xclick\" name=\"_xclick\" target=\"_self\" action=\"http://localhost/HTTPHandleTRIM/UploadHandler.ashx\" method=\"post\">");
strForm.Append("<input type=\"hidden\" name=\"strTrimURL\" value=\"{0}\" />");
strForm.Append("<input type=\"hidden\" name=\"objBinaryData\" value=\"{1}\" />");
strForm.Append("<input type=\"hidden\" name=\"b64fileName\" value=\"{2}\" />");
strForm.Append("<input type=\"hidden\" name=\"strDocument\" value=\"{3}\" />");
strForm.Append("<input type=\"hidden\" name=\"strMetaData\" value=\"{4}\" />");
strForm.Append("</form>");
return String.Format(strForm.ToString()
, txtTrimURL.Text
, objBinaryData.Text
, b64fileName.Text
, txtTrimRecordType.Text
, strMetaData.Text);
}

最佳答案

抱歉,如果我遗漏了什么,但你不能简单地使用纯 HTML 表单将文件上传到你的处理程序:

<form action="UploadHandler.ashx" method="post" enctype="multipart/form-data">
Choose file to upload:
<input name="file" type="file" size="50">
</form>

关于c# - 如何将数据从网络表单页面发布到 HTTPHandler.ashx 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1296781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com