"dojox/form/Uploader",
//上传文件的处理路径
var url = this.projectName + "/widgets/handler/FruitSubmit.ashx?methodName=FileUpload";
this.fileupload = new Uploader({
label:"选择文件",
url: url,
multiple: false,
uploadOnSelect: false
}, this.fileUpLoaderNode);
dojo.connect(this.fileupload, "onComplete", function (e) {
if (e.error == "" || e.error == undefined || e.error == null) {
alert("提交成功!");
}
else {
var errorMsg = e.error.replace(/enter/g, "\n");
alert("提交失败,错误原因:\n" + errorMsg);
}
});
参数在url中携带,中文乱码问题使用encodeURI编码,后台使用System.Web.HttpUtility.UrlDecode(context.Request.Params["cityName"], Encoding.UTF8);解码
this.fileupload.url = this.projectName + "/widgets/handler/FruitSubmit.ashx?methodName=FileUpload&year=" +
this.sltYear.value + "&city=" + this.sltCity.value + "&cityName=" + encodeURI(this.sltCity.focusNode.textContent);;
this.fileupload.upload();
后台返回值为json形式,{"file":"web/a.zip","name":"a.zip","type":"zip"}。另:JSON中不能携带\r\n,在后台使用自定义的取代字符(我这里用的enter),到前台后在进行转换
<span style="white-space:pre"> </span>resultStr.Append("{\"file\":\"");
resultStr.Append(context.Request.ApplicationPath);
resultStr.Append("/" + postedFile.FileName);
resultStr.Append("\",\"name\":\"" + postedFile.FileName);
resultStr.Append("\",\"type\":\"" + postedFile.FileName.Split('.')[1]);
checkResult = checkResult.Replace("\r\n", "enter");
resultStr.Append("\",\"error\":\"" + checkResult + "\"}");
//string str = "{\"file\":\"upload/file.zip\",\"name\":\"file.zip\",\"type\":\"zip\",\"error\":\""+checkResult+"\"}";
//str = str.Replace("\r\n","enter");
context.Response.Write(resultStr.ToString());