我正在開發一個應用程序,使用自定義模式彈出窗口在服務器上傳PDF文件。我正在使用文件上傳瀏覽器html控件上傳.pdf文件,並且該控件是在部分視圖中設計的。當我點擊「添加」按鈕,然後在服務器端,我沒有得到HttpPostedFileBase和FormCollection值。HttpPostedFileBase始終爲無局部視圖
這裏是我的代碼:
管窺:
@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"); });