Jas*_*raj 1 javascript c# asp.net-mvc asp.net-mvc-3
在我的MVC中,我有一个视图,它包含一个文件上传控件和一个按钮.
<input type="file" id="Uploadfile" />
<input type="button" onclick()="GetFile();/>
Run Code Online (Sandbox Code Playgroud)
Javascript函数如下
function GetFile()
{
var file_data = $("#Uploadfile").prop("files")[0];
window.location.href="Calculation/Final?files="+file_data;
}
Run Code Online (Sandbox Code Playgroud)
我需要通过fileupload控件将所选文件传递/发送到mvc中的控制器.我有控制器
public ActionResult Final(HttpPostedFileBase files)
{
//here i have got the files value is null.
}
Run Code Online (Sandbox Code Playgroud)
如何获取所选文件并将其发送到控制器?Plz帮我解决了这个问题.
我在项目中有类似的功能.工作代码看起来像这样:
[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();
}
Run Code Online (Sandbox Code Playgroud)
@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>
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
26613 次 |
| 最近记录: |