关于使用客户端控件和jquery上传文件

一.导入Jquery插件ajaxfileupload.js
使用方法:
$.ajaxFileUpload({
//type: "post",
fileElementId: "importFile",
url: "../ashx/BaseInfo/StationManage.ashx",
data: { parapath: "i", action: "import" },
dataType: "json",
secureuri: false,
success: function (json) {
},
error: function () {
Common.ShowAlert("提示", "访问服务器失败或数据返回格式错误", function () { });
}
});

 

注意:需要写在全局JS中,不能写在其他方法中,否则无效;

另,input需要有name,和ID一样(不一样的情况没测试过),否则无效

二.使用input:

<p><span class="label">选择文件:</span><input type="file" id="importFile" name="importFile" /></p>

 

三.后台获取:

HttpFileCollection postedFile = context.Request.Files;
HttpFileCollection postedFile2 = HttpContext.Current.Request.Files;
string savePath = context.Server.MapPath(("/UploadFile\\") + postedFile[0].FileName);//Server.MapPath 获得虚拟服务器相对路径

postedFile[0].SaveAs(savePath);

 

结束
 
补充可能出现的问题
上传成功后一直进error,进不了success
网上说是因为Server端的Response上加上了contentType
于是多了<pre>的标签,于是修改了ajaxUpLoad里面uploadHttpData
的源码 ,修改如下:
uploadHttpData: function( r, type ) {
var data = !type;
data = type == "xml" || data ? r.responseXML : r.responseText;
// If the type is "script", eval it in global context
if ( type == "script" )
jQuery.globalEval( data );
// Get the JavaScript object, if JSON is used.
if (type == "json")
////////////以下为新增代码/////////////// 
data = r.responseText;
var start = data.indexOf(">");
if (start != -1) {
var end = data.indexOf("<", start + 1);
if (end != -1) {
data = data.substring(start + 1, end);
}
}
///////////以上为新增代码/////////////// 
eval( "data = " + data );
//eval("data = \" " + data + " \" ");
// evaluate scripts within html
if ( type == "html" )
jQuery("<div>").html(data).evalScripts();

return data;
}

另还查到另一种是把eval( "data = " + data );改成eval("data = \" " + data + " \" "); 不过对我无效

posted @ 2014-03-26 00:24  玢棂  阅读(374)  评论(0编辑  收藏  举报