封装好的jq插件
(function () {
var iframe = '<iframe name="jqUploadIframe" style="display: none"></iframe>';
function getSearchObjct(window) {
var search;
try {
search = window.location.search.substr(1);
} catch (e) {
return null;
}
if (search == "") return "";
var keyValue = search.split("&");
var result = {};
for (var i = 0; i < keyValue.length; i++) {
var onek = keyValue[i].split("=");
result[onek[0]] = decodeURI(onek[1]);
}
return result;
}
function getRootPath() {
var curWwwPath = window.document.location.href;
var pathName = window.document.location.pathname;
var pos = curWwwPath.indexOf(pathName);
var localhostPaht = curWwwPath.substring(0, pos);
var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
return (localhostPaht + projectName);
}
var methods = {
//跨域上传
crossDomain: function (op) {
this.attr("target", "jqUploadIframe");
var $iframe = $(iframe);
var val = "window.location.href='" + getRootPath() + "/jqUpload32495982348952?data=massage'";
this.append('<input type="hidden" class="jqUpload-localhostname" ' +
'name="uploadCall" value="' + val + '"/>');
$iframe[0].onload = function () {
var searchData = getSearchObjct(this.contentWindow);
if (searchData == "") {
return;
} else if (searchData == null) {
op.error();
} else if (typeof searchData == "object") {
op.success(searchData.data);
}
$iframe.remove();
$(".jqUpload-localhostname").remove();
}
this.append($iframe);
$(this).submit();
}
}
$.fn.jqAjaxUpload = function (op) {
methods.crossDomain.apply(this, [op])
}
})()
前台调用
//需要提交的表单
var form = $("<form action='http://www.cnblogs.com/GoCircle/SaveUploadedFile' method='post' enctype='multipart/form-data'></form>"); form.append($("<input name='formFile' value='" + clearBase64 + "' />"));
form.append($("<input name='filename' value='" + fileName + "' />"));
form.append($("<input name='maintainID' value='" + maintainid + "' />"));
$('#iframeArea').append(form);//必须要追加到当前html页中
$(form).jqAjaxUpload({
isCrossDomain: true,
success: function (result) {
},
error: function () {
}
})
后台C# mvc
[AllowAnonymousAttribute]//允许所有请求
[HttpPost]
public ActionResult SaveUploadedFile(String formFile, String filename, String maintainID)
{
//这里必须为这种格式 return Content("<script>" + Request["uploadCall"].Replace("massage", isSavedSuccessfully) + "</script>");
}