我正在开发一个应用程序,用于使用自定义模式弹出在服务器上上传 pdf 文件。我正在使用文件上传浏览器 html 控件来上传 .pdf 文件,并且该控件是在部分视图中设计的。当我单击“添加”按钮时,在服务器端我没有得到 HttpPostedFileBase 和 FormCollection 值。
这是我的代码:
局部视图:
@model Zytron.Models.ProductDataControls
@using (Html.BeginForm("UploadFiles", "AdminPanel", FormMethod.Post, new
{
@id = "file_upload",
}))
{
<table width="100%" cellpadding="5" cellspacing="1">
<tr>
<td>
<span>Description</span>
</td>
</tr>
<tr>
<td>
@Html.TextBoxFor(m => m.Description, new
{
@class = "textBox"
})
@Html.HiddenFor(m => m.ProductId)
@Html.HiddenFor(m => m.ParentId)
</td>
</tr>
<tr>
<td>
<span>File Source</span>
</td>
</tr>
<tr>
<td>
<input type="file" id="fileUpload" name="fileUpload" size="23" />
</td>
</tr>
</table>
}
型号代码:
public class ProductDataControls
{
public string Description { get; set; }
}
自定义模式弹出代码:
function loadProdAttachFile(tag, event, target)
{
event.preventDefault();
var $loading = $('<img src="@Url.Content("~/Images/ajaxLoading.gif")" alt="loading" class="ui-loading-icon">');
var $url = $(tag).attr('href');
var $title = $(tag).attr('title');
var $dialog = $('<div></div>');
$dialog.empty();
// alert($url);
$dialog
.append($loading)
.load($url)
.dialog({
autoOpen: false
, title: $title
, width: 500
, modal: true
, minHeight: 220
, show: 'fade'
, hide: 'fade'
});
$dialog.dialog("option", "buttons", {
"Add": function () {
var dlg = $(this);
//$('form#file_upload').submit();
var file = document.getElementById("fileUpload").value;
var pid = getParamValue("pid", $url);
var type = getParamValue("type", $url);
$.ajax({
url: '/AdminPanel/UploadFiles',
type: 'POST',
data: { 'file': file, 'pid' : pid, 'type' : type },
success: function (response) {
dlg.dialog('close');
dlg.empty();
},
error: function (xhr) {
if (xhr.status == 400)
dlg.html(xhr.responseText, xhr.status); /* display validation errors in edit dialog */
else
displayError(xhr.responseText, xhr.status); /* display other errors in separate dialog */
}
});
},
"Cancel": function () {
$(this).dialog("close");
$(this).empty();
}
});
$dialog.dialog('open');
};
控制器代码:
[HttpPost]
public void UploadFiles(HttpPostedFileBase file, FormCollection form)
{
}
查看代码:
<a href "/ UploadFiles” class="ModalDlgProdAttachment" <img src=”../Images/MyImage.jpg" /> </a>
$('a. ModalDlgProdAttachment).live("click", function (event) { loadProdAttachFile(this, event, "# file_upload"); });