0
我想在MVC3中使用模態彈出來實現文件上傳。上傳文件時,控制器接收空值而不是文件名。這個功能是否可以在MVC3中實現。
jQuery是如下,在MVC3中使用模式彈出的文件上傳
控制器代碼:
「
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
var fileName = "";
var path = "";
string file1 = "";
if (ModelState.IsValid)
{
if (file.ContentLength > 0)
{
fileName = Path.GetFileName(file.FileName);
path = Path.Combine(Server.MapPath("~/Content/"), fileName);
file.SaveAs(path);
}
}
return RedirectToAction("Create","Manager",new{file1=fileName});
}
」 「
var linkObj;
$(function() {
$(".editLink").button();
$('#updateDialog').dialog({
autoOpen: false,
width: 400,
resizable: false,
modal: true,
buttons: {
"Update": function() {
$("#update-message").html(''); //make sure there is nothing on the message before we continue
$("#updateCarForm").submit();
},
"Cancel": function() {
$(this).dialog("close");
}
}
});
$(".editLink").click(function() {
//change the title of the dialgo
linkObj = $(this);
var dialogDiv = $('#updateDialog');
var viewUrl = linkObj.attr('href');
$.get(viewUrl, function (data) {
dialogDiv.html(data);
//validation
var $form = $("#updateCarForm");
// Unbind existing validation
$form.unbind();
$form.data("validator", null);
// Check document for changes
$.validator.unobtrusive.parse(document);
// Re add validation with changes
$form.validate($form.data("unobtrusiveValidation").options);
//open dialog
dialogDiv.dialog('open');
});
return false;
});
});
function updateSuccess() {
if ($("#update-message").html() == "True") {
//we update the table's info
var parent = linkObj.closest("tr");
parent.find(".carName").html($("#Name").val());
parent.find(".carDescription").html($("#Description").val());
//now we can close the dialog
$('#updateDialog').dialog('close');
//twitter type notification
$('#commonMessage').html("Update Complete");
$('#commonMessage').delay(400).slideDown(400).delay(3000).slideUp(400);
}
else {
$("#update-message").show();
}
}
</script>
」
腳本加載文件。
「
<% using (Ajax.BeginForm("Upload", "Manager", null,
new AjaxOptions
{
UpdateTargetId = "update-message",
InsertionMode = InsertionMode.Replace,
HttpMethod = "POST",
OnSuccess = "updateSuccess"
}, new { @id = "updateCarForm" }))
{ %>
<div class="editor-field" id="error invisible"></div>
<fieldset>
<div>
<input type="file" name="file" id="file"/>
</div>
<div>
<input type="submit" value="Save" />
</div>
</fieldset>
<% } %>
」
控制器代碼請 – 2012-07-14 17:39:27
[HttpPost] 公共的ActionResult上傳(HttpPostedFileBase文件) { 變種文件名= 「」; var path =「」; string file1 =「」; (file.ContentLength> 0) { fileName = Path.GetFileName(file.FileName);如果(ModelState.IsValid) if(ModelState.IsValid) if path = Path.Combine(Server.MapPath(「〜/ Content /」),fileName); file.SaveAs(path); } } return RedirectToAction(「Create」,「Manager」,new {file1 = fileName}); } – Roman 2012-07-14 18:17:09
你有asp-mvc3作爲標籤..但你使用mvc2? – 2012-07-16 14:18:35