$('#aImport').uploadify({
'removeCompleted': true,
'height': 25,
'width': 45,
'buttonText': '匯入',
'queueID': 'queue',
'multi': false,
'uploader': '@Url.Action("ImportVistorFile")',
'swf': '@Url.Content("~/Content/flash/uploadify.swf")',
'fileObjName': 'file',
'onUploadSuccess': function (file, data, response) {
alert("匯入成功");
},
'onUploadError': function (file, errorCode, errorMsg, errorString) {
alert('匯入失敗');
}
});
後端程式的接法如下,直接接HttpPostedFileBase
public ActionResult ImportVistorFile(HttpPostedFileBase file)
{
if (file != null)
{
var fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + file.FileName;
var savePath = Path.Combine("D:/", fileName);
file.SaveAs(savePath);
}
}
但有時候除了檔案之外,還需要傳入其他東西
範例如下
public ActionResult ImportVistorFile(HttpPostedFileBase file,int projId)
這時候uploadify可以加入formData
以JSON的方式傳入參數
例如
'formData': { "projId": $('#ProjId').val() },
整體範例如下
$('#aImport').uploadify({
'removeCompleted': true,
'height': 25,
'width': 45,
'buttonText': '匯入',
'queueID': 'queue',
'multi': false,
'uploader': '@Url.Action("ImportVistorFile")',
'swf': '@Url.Content("~/Content/flash/uploadify.swf")',
'fileObjName': 'file',
'formData': { "projId": $('#ProjId').val() },
'onUploadStart' : function(file) {
$("#aImport").uploadify("settings", "formData", { "projId": $("#ProjId").val() });
},
'onUploadSuccess': function (file, data, response) {
alert("匯入成功");
},
'onUploadError': function (file, errorCode, errorMsg, errorString) {
alert('匯入失敗');
}
});
2015/11/19 更新
今天又遇到不可思議的問題
在使用下拉選單傳入ProjId參數時
不管怎麼怎麼怎麼選,永遠都只傳入第一個選單
搞了一上午,總算解決了
在onUploadStart時重新設定formData的內容
'onUploadStart' : function(file) {
$("#aImport").uploadify("settings", "formData", { "projId": $("#ProjId").val() });
},
沒有留言:
張貼留言