在我的MVC中,我有一个视图,它包含一个文件上传控件和一个按钮。
<input type="file" id="Uploadfile" />
<input type="button" onclick()="GetFile();/>
Javascript功能如下
function GetFile()
{
var file_data = $("#Uploadfile").prop("files")[0];
window.location.href="Calculation/Final?files="+file_data;
}
我需要通过fileupload控件将所选文件传递给mvc中的控制器。 我有控制器
public ActionResult Final(HttpPostedFileBase files)
{
//here i have got the files value is null.
}
如何获取所选文件并将其发送到控制器? Plz帮我解决了这个问题。
答案 0 :(得分:6)
我在项目中提供了类似的功能。 工作代码看起来像这样:
[HttpPost]
public ActionResult UploadFile(YourModel model1)
{
foreach (string file in Request.Files)
{
HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
if (hpf.ContentLength > 0)
{
string folderPath = Server.MapPath("~/ServerFolderPath");
Directory.CreateDirectory(folderPath);
string savedFileName = Server.MapPath("~/ServerFolderPath/" + hpf.FileName);
hpf.SaveAs(savedFileName);
return Content("File Uploaded Successfully");
}
else
{
return Content("Invalid File");
}
model1.Image = "~/ServerFolderPath/" + hpf.FileName;
}
//Refactor the code as per your need
return View();
}
@using (@Html.BeginForm("UploadFile", "Upload", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<table style="border: solid thin; margin: 10px 10px 10px 10px">
<tr style="margin-top: 10px">
<td>
@Html.Label("Select a File to Upload")
<br />
<br />
<input type="file" name="myfile">
<input type="submit" value="Upload" />
</td>
</tr>
</table>
}
答案 1 :(得分:2)
你不能通过javascript发送文件内容(除非HTMl5)。而你完全错了。如果你想通过FileReader api做基于HTML5的解决方案,那么你需要检查一下。 FileReader Api
只需放置一个表单标记,并在控制器操作中使用相同的输入名称来执行模型绑定
@using(Html.BeginForm("yourAction","YourControl",FormMethod.Post))
{
<input type="file" id="fileUpload" />
}
然后在控制器中。
[HTTPPost]
public ActionResult Final(HttpPostedFileBase fileUpload)
{
//here i have got the files value is null.
}
答案 2 :(得分:0)
作为Ravi答案的完成,我建议使用以下using
声明:
@using(Html.BeginForm("yourAction","YourControl",FormMethod.Post, new { enctype="multipart/form-data" }))
{
<input type="file" id="fileUpload" />
}
答案 3 :(得分:0)
您可以使用json数据进行查看。
例如,
控制器
public ActionResult Products(string categoryid)
{
List<catProducts> lst = bindProducts(categoryid);
return View(lst);
}
public JsonResult Productsview(string categoryid)
{
//write your logic
var Data = new { ok = true, catid = categoryid};
return Json(Data, JsonRequestBehavior.AllowGet);
}
查看:
@{
ViewBag.Title = "Index";
}
@model ASP.NETMVC.Controllers.Categories
<h2>List Of Categories</h2>
@Html.ListBox("lst_categories", (IEnumerable<SelectListItem>) ViewBag.Categories)
<script type="text/javascript">
$(function () {
$('#lst_categories').change(function () {
var catid = $('#lst_categories :selected').val();
$.ajax({
url: '@Url.Action("Productsview", "Jquery")',
type: 'GET',
dataType: 'json',
data: { categoryid: catid },
cache: false,
success: function (Data) {
if (Data.ok) {
var link = "@Url.Action("Products", "Jquery", new { categoryid = "catid" })";
link = link.replace("catid", Data.catid);
alert(link);
window.location.href = link;
}
}
});
});
});
</script>
答案 4 :(得分:0)
下面的代码将以隐藏的形式执行完整的回发,这将给出ajax文件上传的错觉。试试吧
<强>更新强>
<强> JS 强>
function Upload(sender) {
var iframe = $("<iframe>").hide();
var newForm = $("<FORM>");
newForm.attr({ method: "POST", enctype: "multipart/form-data", action: "/ControllerName/Final" });
var $this = $(sender), $clone = $this.clone();
$this.after($clone).appendTo($(newForm));
iframe.appendTo($("html")).contents().find('body').html($(newForm));
newForm.submit();
}
<强> HTML 强>
<input type="file" id="Uploadfile" name="Uploadfile" />
<input type="button" onclick="Upload($('#UploadFile'));/>
<强>控制器强>
public ActionResult Final(HttpPostedFileBase Uploadfile)
{
//here i have got the files value is null.
}