使用 jquery 和 handler(ashx) 上传文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8466941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
upload file using jquery and handler(ashx)
提问by user1092626
I am trying to upload a file using jquery ajax with handler (c#). The problem is, when I call the handler I get
我正在尝试使用带有处理程序 (c#) 的 jquery ajax 上传文件。问题是,当我调用处理程序时,我得到
context.Request.File.Count=0
Here is the aspx code:
下面是aspx代码:
<!--aspx file code-->
<script language="javascript" type="text/javascript">
$().ready(function ()
{
$('#save').click(function (e)
{
CalluploaderHandler();
});
});
function CalluploaderHandler()
{
$.ajax({
type: "POST",
url: "Services/UPloader.ashx",
contentType: "application/json; charset=utf-8",
success: OnComplete,
error: OnFail
});
return false;
}
function OnComplete(result)
{
alert('Success');
}
function OnFail(result)
{
alert('Request failed');
}
</script>
</head>
<body>
<form enctype="multipart/form-data">
<label for="file">
Filename:</label>
<input name="file" id="file" type="file">
<input id="save" name="submit" value="Submit" type="submit">
</form>
</body>
</html>
The c# code handler:
C# 代码处理程序:
/* handler*/
public void ProcessRequest(HttpContext context)
{
string savedFileName = "";
foreach (string file in context.Request.Files)
{
HttpPostedFile hpf = context.Request.Files[file] as HttpPostedFile;
if (hpf.ContentLength == 0)
continue;
// savedFileName = context.Server.MapPath(Path.GetFileName(hpf.FileName));
// hpf.SaveAs(savedFileName);
}
context.Response.Write(savedFileName);
}
回答by Rafay
i think the problem is with the contentTypetry
我认为问题在于contentType尝试
contentType: 'multipart/form-data',
OR
或者
contentType :'application/octet-stream';
see this post for more information
看这篇文章了解更多信息
回答by Venugopal M
You can add this kind of code to the handler file. Then you can post to this url(whateverroot/yourhandler.ashx)
您可以将这种代码添加到处理程序文件中。然后你可以发布到这个 url(whateverroot/yourhandler.ashx)
The content type should be "multipart/form-data". For eg: If you're using a HTML form tag, then enctype="multipart/form-data".
内容类型应该是“multipart/form-data”。例如:如果您使用 HTML 表单标签,则 enctype="multipart/form-data"。
public void ProcessRequest(HttpContext context)
{
var result = "0";
try
{
if (context.Request.Files.Count > 0)
{
HttpPostedFile file = null;
for (int i = 0; i < context.Request.Files.Count; i++)
{
file = context.Request.Files[i];
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(<somepath>, fileName);
file.SaveAs(path);
result = "1";
}
}
}
}
catch { }
context.Response.Write(result);
}
回答by Ranjan Shetty
Your code...
你的代码...
$.ajax({
type: "POST",
url: "Services/UPloader.ashx",
contentType: "application/json; charset=utf-8",
success: OnComplete,
error: OnFail
});
..is missing the dataparameter. The way it's currently written, nothing is being sent to the handler.
..缺少data参数。它目前的编写方式,没有任何东西被发送到处理程序。
You need to pass the file to the handler, using the dataparameter.
Please have a go through this link: http://www.aspdotnet-suresh.com/2015/02/jquery-upload-images-files-without-page-refresh-postaback-in-aspnet.html
您需要使用data参数将文件传递给处理程序。请浏览此链接:http: //www.aspdotnet-suresh.com/2015/02/jquery-upload-images-files-without-page-refresh-postaback-in-aspnet.html

