easyuiForm提交:
前台代码:
<form id="importFileForm" method="post" enctype="multipart/form-data" style="display:">
<table style="margin:5px;height:70px;">
<tr>
<td></td>
<td width="5px;"></td>
<td><input class="easyui-filebox" id="fileImport" data-options="buttonText:'选择',prompt:'请选择文件...'" name="fileImport" style="width:260px;">
</td>
<td><a id="uploadFile" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" href="javascript:void(0)">上传</a></td>
</tr>
<tr>
<td colspan="4">
<label id="uploadInfo" />
</td>
</tr>
</table>
</form>
<script>
//导入文件
$("#uploadFile").click(function () {
var formData = new FormData($("#importFileForm")[0]);
//调用apicontroller后台action方法,将form数据传递给后台处理。contentType必须设置为false,否则chrome和firefox不兼容
$.ajax({
url: "http://localhost:12745/api/easyuiUpload/PostExcelData",
type: 'POST',
data: formData,
async: false,
cache: false,
contentType: false,
processData: false,
success: function (returnInfo) {
//上传成功后将控件内容清空,并显示上传成功信息
document.getElementById('fileImport').value = null;
document.getElementById('uploadInfo').innerHTML = "<span style='color:Red'>" + returnInfo + "</span>";
},
error: function (returnInfo) {
//上传失败时显示上传失败信息
document.getElementById('uploadInfo').innerHTML = "<span style='color:Red'>" + returnInfo + "</span>";
}
});
})
</script>
后台代码:
[HttpPost]
public string PostExcelData()
{
string info = string.Empty;
try
{
//获取客户端上传的文件集合
HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
//判断是否存在文件
if (files.Count > 0)
{
//获取文件集合中的第一个文件(每次只上传一个文件)
HttpPostedFile file = files[0];
//定义文件存放的目标路径
string targetDir = System.Web.HttpContext.Current.Server.MapPath("~/FileUpLoad/Product");
//创建目标路径
if (!Directory.Exists(targetDir))
{
Directory.CreateDirectory(targetDir);
}
//ZFiles.CreateDirectory(targetDir);
//组合成文件的完整路径
string path = System.IO.Path.Combine(targetDir, System.IO.Path.GetFileName(file.FileName));
//保存上传的文件到指定路径中
file.SaveAs(path);
info = "上传成功";
}
else
{
info = "上传失败";
}
}
catch
{
info = "上传失败";
}
return info;
}
easyui另类Form提交(前台页不需要form表单):
前台代码:
<script type="text/javascript">
$.support.cors = true;
var ApiUrl = "http://localhost:12745/";
$(function () {
$("#upload").click(function () {
$("#imgWait").show();
var formData = new FormData();
formData.append("myfile", $("#file1").next().find('input[id^="filebox_file_id_"]')[0].files[0]);
formData.append("myfile2", $("#file2").next().find('input[id^="filebox_file_id_"]')[0].files[0]);
$.ajax({
url: ApiUrl + "api/FileManage/UploadFile",
type: "POST",
data: formData,
/**
*必须false才会自动加上正确的Content-Type
*/
contentType: false,
/**
* 必须false才会避开jQuery对 formdata 的默认处理
* XMLHttpRequest会对 formdata 进行正确的处理
*/
processData: false,
success: function (data) {
if (data.status == "true") {
alert("上传成功!");
}
if (data.status == "error") {
alert(data.msg);
}
$("#imgWait").hide();
},
error: function () {
alert("上传失败!");
$("#imgWait").hide();
}
});
});
});
</script>
</head>
<body>
<!--选择文件:<input type="file" id="file1" /><br />-->
<input class="easyui-filebox" id="file1" data-options="prompt:'Choose another file...'" style="width:150px">
<input class="easyui-filebox" id="file2" data-options="prompt:'Choose another file...'" style="width:150px">
<input type="button" id="upload" value="上传" />
<img src="wait.gif" style="display:none" id="imgWait" />
</body>
后台代码:
[HttpPost]
public HttpResponseMessage UploadFile()
{
HttpResponseMessage result = null;
var httpRequest = System.Web.HttpContext.Current.Request;
if (httpRequest.Files.Count > 0)
{
try
{
string url = string.Empty;
foreach (string file in httpRequest.Files)
{
var postedFile = httpRequest.Files[file];
var filePath = System.Web.HttpContext.Current.Server.MapPath("~/Files/");
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
postedFile.SaveAs(filePath + postedFile.FileName);
url += "Files/" + postedFile.FileName + ",";
}
result = Request.CreateResponse(HttpStatusCode.OK, url.Substring(0, url.Length - 1));
}
catch (Exception ex)
{
result = Request.CreateResponse(HttpStatusCode.OK, "error:" + ex.Message);
}
}
else
{
result = Request.CreateResponse(HttpStatusCode.OK, "0");
}
return result;
}