在asp.net中使用jquery使用webservice上传文件的问题 [英] problem with uploading file using webservice using jquery in asp.net
本文介绍了在asp.net中使用jquery使用webservice上传文件的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用webservice上传doc和pdf文件。
我有这样的错误
I am trying to upload doc and pdf files using webservice.
I have an error like this
System.ArgumentOutOfRangeException was unhandled by user code
HResult=-2146233086
Message=Index was out of range. Must be non-negative and less than the size of the collection.
我的代码是
my code is
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string uploadfile(string res)
{
HttpContext postedContext = HttpContext.Current;
HttpPostedFile file = postedContext.Request.Files[0];
//HttpPostedFile file = HttpContext.Current.Request.Files[formData];
//HttpPostedFile hFile = postedContext.Request.Files[1];
string name = file.FileName;
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file.FileName);
string fileExtension = Path.GetExtension(file.FileName);
//file.PostedFile.SaveAs("d:\\Resumes\\" + ISO_Date() + "_" + fileNameWithoutExtension + fileExtension);
//File.Copy("d:\\Resumes\\" + ISO_Date() + "_" + fileNameWithoutExtension + fileExtension);
file.SaveAs("d:\\Resumes\\" + ISO_Date() + "_" + fileNameWithoutExtension + fileExtension);
return res;
}
static String ISO_Date()
{
return DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss");
}
jquery函数
jquery function
<script type="text/javascript">
function finalupload() {
var res = document.getElementById('txtSocialPassword').value;
var newfile = document.getElementById("<%=file.ClientID%>").files[0];
alert(formData);
//alert(res);
$.ajax({
type: "POST",
contentType: "application/json",
url: "HuntableWebService.asmx/uploadfile",
data: "{'res':'" + res + "'}",
dataType: "JSON",
async: true,
success: function (response) {
//var res = response.d;
alert(response.d);
}
});
return false;
}
</script>
我正在尝试将简历保存到硬盘文件夹中。
please帮助我。
谢谢....
I am trying to save resume in to harddisk folder.
please help me.
Thank you....
推荐答案
.ajax({
type:POST,
contentType:application / json,
url:HuntableWebService.asmx / uploadfile,
data:{'res':'+ res +'},
dataType:JSON,
async:true,
success:function(response){
// var res = response.d;
alert(response.d);
}
});
返回false;
}
< / script >
.ajax({ type: "POST", contentType: "application/json", url: "HuntableWebService.asmx/uploadfile", data: "{'res':'" + res + "'}", dataType: "JSON", async: true, success: function (response) { //var res = response.d; alert(response.d); } }); return false; } </script>
我正在尝试将简历保存到硬盘文件夹中。
please帮助我。
谢谢....
I am trying to save resume in to harddisk folder.
please help me.
Thank you....
把你的
var res
放在
in
Sys.Serialization.JavaScriptSerializer.serialize();
之后尝试你的代码...
after that try your code...
.ajax ({
类型: POST,
contentType: application / json,
url: HuntableWebService.asmx / uploadfile,
data: {'res':' + Sys.Serialization.JavaScriptSerializer.serialize(res)+ '},
dataType: JSON,
异步: true ,
成功:功能(响应){
< span class =code-comment> // var res = response.d;
alert(response.d);
}
});
.ajax({ type: "POST", contentType: "application/json", url: "HuntableWebService.asmx/uploadfile", data: "{'res':'" + Sys.Serialization.JavaScriptSerializer.serialize(res) + "'}", dataType: "JSON", async: true, success: function (response) { //var res = response.d; alert(response.d); } });
这篇关于在asp.net中使用jquery使用webservice上传文件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文