第一、cshtml文件代码:
class="easyui-window" id="import-excel-template" title="文件上传" style="width:500px;height:150px;padding:2px;" closed="true">
《Form id="importFileForm" method="post" enctype="multipart/form-data" style="display:none">
《 table style="margin:5px;height:70px;">
《tr>
《td>《input type="file" class="easyui-filebox" id="fileImport" name="fileImport" style="width:400px;" onchange="document.getElementById('textfield').value=this.value">
《td>
《/tr>
《tr>
《td colspan="4">《label id="fileName" />
《/tr>
《tr>
《td colspan="4">
《label id="uploadInfo" />
《/tr>
《/table>
《input type="text" name="textfield" class="file" id="textfield"
《div style="text-align:center;clear:both;margin:5px;">
《a id="uploadFile2" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" data-bind="click:importFileClick" href="javascript:void(0)">上传</a>
《a class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" data-bind="click:closeImportClick" href="javascript:void(0)">关闭</a>
《/div>
《/form>
《/div>
第二、js代码
//获取form数据
var formData = new FormData($("#importFileForm")[0]);
var strUrl = '/api/sys/ProductVersion/PostExcelData?' + 'VersionCode=' + escape(mUploadVersion);//关键 escape编码后台直接读出的是中文
//调用apicontroller后台action方法,将form数据传递给后台处理。contentType必须设置为"multipart/form-data"
$.ajax({
url: strUrl,
type: 'POST', //上传文件则此属性是必须的,但得出VersionCode为乱码;若不是上传文件不要此属性VersionCode不是乱码
data: formData, //{"":"avvvv"}, //与此属性无关Request.Files
async: false,
cache: false,
contentType: "multipart/form-data", //必须false才会自动加上正确的Content-Type
processData: false, //必须false才会避开jQuery对 formdata 的默认处理
success: function (returnInfo) {
//上传成功后将控件内容清空,并显示上传成功信息
document.getElementById('fileImport').value = null;
document.getElementById('uploadInfo').innerHTML = "<span style='color:Red'>" + returnInfo + "</span>";
mbHasUpload = true;
},
error: function (returnInfo) {
//上传失败时显示上传失败信息
document.getElementById('uploadInfo').innerHTML = "<span style='color:Red'>" + returnInfo + "</span>";
}
});
第三、public class ProductVersionApiController : ApiController里面的方法PostExcelData()
//todo 获取客户端上传的文件集合, ie11可以,360-火狐-google chromse不行
HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
问题:Request.Files里面得到的是文件的路径而且除了ie其他浏览器时个数都是0,这个方法不实用。如果能得到文件字节流最好了。
不过无论什么方法,能解决我的问题就行