<form id="UploadForm" action="UploadFileServer.axd" method="post" enctype="multipart/form-data" runat="server">
<div class="folderSelectorCont">
<div class="select_file_ttl">${resource.upload_file_Select_File}:</div>
<div class="folderNameInputArea">
<button class="select_file_btn" id="choose_file">Select</button>
<input type="file" class="hidden_input" id="file"/>
<label class="chosen_folder" id="file_name" title=""></label>
</div>
</div>
<div class="popButtonsCont">
<a class="dialogBtns" onclick="hideDialog(); return false;">${resource.manageFolder_cancel}</a>
<a class="dialogBtns" id="createBtn">${resource.manageFolder_ok}</a>
</div>
</form>
UploadFileServer.axd:
void IHttpHandler.ProcessRequest(HttpContext ctx)
{
HttpFileCollection uploadFile = ctx.Request.Files;
if (uploadFile.Count > 0)
{
//do something
ctx.Response.ContentType = "application/json; charset=utf-8";
ctx.Response.Write(uploadFileResponse);
}
但我得到uploadFile.Count = 0。 为什么呢?
答案 0 :(得分:0)
发布表单数据时,控件必须具有name属性,因为数据是以名称 - 值对的形式发送的,因此首先尝试在输入中添加名称:
<input type="file" class="hidden_input" id="file" name="file" />
答案 1 :(得分:0)
正如这家伙指出:http://www.prolistingservice.com/theblog/post/HttpFileCollection-Always-Zero.aspx
摘录:
我发现的每个网站都说有两件事需要你来填充HttpFileCollection。
1)确保表单方法为“POST” 2)确保表单enctype是“multipart / form-data”
AjaxFileUpload代码尽职尽责,但是还有一件事是&gt; ASP.NET需要。
3)HTML文件输入必须具有“name”属性。
我已经使用您提供的代码创建了一个测试项目,注册了HttpHandler,并且一旦我将“name”属性添加到input元素就可以了。
以下是代码,请注意添加到输入元素中的名称属性:
<强>的Web.config:强>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.webServer>
<handlers>
<add name="uploadHandler" path="UploadFileServer.axd" verb="*" type="FileUploadHandler.IISHandler1, FileUploadHandler" />
</handlers>
</system.webServer>
</configuration>
HTTP处理程序:
namespace FileUploadHandler
{
public class IISHandler1 : IHttpHandler
{
public bool IsReusable { get { return false; } }
void IHttpHandler.ProcessRequest(HttpContext ctx)
{
HttpFileCollection uploadFile = ctx.Request.Files;
string uploadFileResponse = "no count";
if (uploadFile.Count > 0)
{
uploadFileResponse = "count > 0";
}
ctx.Response.ContentType = "application/json; charset=utf-8";
ctx.Response.Write(uploadFileResponse);
}
}
}
页面:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="UploadForm" action="UploadFileServer.axd" method="post" enctype="multipart/form-data" runat="server">
<div class="folderSelectorCont">
<div class="select_file_ttl">${resource.upload_file_Select_File}:</div>
<div class="folderNameInputArea">
<button class="select_file_btn" id="choose_file">Select</button>
<input type="file" class="hidden_input" id="file" name="file"/>
<label class="chosen_folder" id="file_name" title=""></label>
</div>
</div>
<div class="popButtonsCont">
<a class="dialogBtns" onclick="hideDialog(); return false;">${resource.manageFolder_cancel}</a>
<a class="dialogBtns" id="createBtn">${resource.manageFolder_ok}</a>
</div>
</form>
</body>
</html>
答案 2 :(得分:0)
正如我在评论中提到的,已知jQuery mobile在这种情况下会引起问题,请将此另一个问题视为example。更新你的配置以关闭jQuery mobile的ajax然后它应该工作。