asp.net-mvc MVC 4 Razor 文件上传
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15680629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MVC 4 Razor File Upload
提问by Cristi Pufu
I am new to MVC 4 and I am trying to implement File Upload Control in my website. I am not able to find the mistake.I am getting a null value in my file.
我是 MVC 4 的新手,我正在尝试在我的网站中实现文件上传控制。我找不到错误。我在我的文件中得到一个空值。
Controller:
控制器:
public class UploadController : BaseController
{
public ActionResult UploadDocument()
{
return View();
}
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/Images/"), fileName);
file.SaveAs(path);
}
return RedirectToAction("UploadDocument");
}
}
View:
看法:
@using (Html.BeginForm("Upload", "Upload", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="FileUpload" />
<input type="submit" name="Submit" id="Submit" value="Upload" />
}
回答by Cristi Pufu
The Uploadmethod's HttpPostedFileBaseparameter must have the same name as the the file input.
该Upload方法的HttpPostedFileBase参数必须与file input.
So just change the input to this:
所以只需将输入更改为:
<input type="file" name="file" />
Also, you could find the files in Request.Files:
此外,您可以在Request.Files以下位置找到文件:
[HttpPost]
public ActionResult Upload()
{
if (Request.Files.Count > 0)
{
var file = Request.Files[0];
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/Images/"), fileName);
file.SaveAs(path);
}
}
return RedirectToAction("UploadDocument");
}
回答by Bishoy Hanna
Clarifying it. Model:
澄清一下。模型:
public class ContactUsModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public HttpPostedFileBase attachment { get; set; }
Post Action
发布操作
public virtual ActionResult ContactUs(ContactUsModel Model)
{
if (Model.attachment.HasFile())
{
//save the file
//Send it as an attachment
Attachment messageAttachment = new Attachment(Model.attachment.InputStream, Model.attachment.FileName);
}
}
Finally the Extension method for checking the hasFile
最后是检查hasFile的Extension方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace AtlanticCMS.Web.Common
{
public static class ExtensionMethods
{
public static bool HasFile(this HttpPostedFileBase file)
{
return file != null && file.ContentLength > 0;
}
}
}
回答by Jagadisha B S
View Page
查看页面
@using (Html.BeginForm("ActionmethodName", "ControllerName", FormMethod.Post, new { id = "formid" }))
{
<input type="file" name="file" />
<input type="submit" value="Upload" class="save" id="btnid" />
}
script file
脚本文件
$(document).on("click", "#btnid", function (event) {
event.preventDefault();
var fileOptions = {
success: res,
dataType: "json"
}
$("#formid").ajaxSubmit(fileOptions);
});
In Controller
在控制器中
[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase file)
{
}
回答by Hafiz Asad
you just have to change the name of your input filed because same name is required in parameter and input field name just replace this line Your code working fine
您只需要更改输入字段的名称,因为参数和输入字段名称中需要相同的名称只需替换这一行您的代码工作正常
<input type="file" name="file" />
回答by Petr Tomá?ek
I think, better way is use HttpPostedFileBasein your controller or API. After this you can simple detect size, type etc.
我认为,更好的方法是在您的控制器或 API 中使用HttpPostedFileBase。在此之后,您可以简单地检测大小、类型等。
File properties you can find here:
您可以在此处找到文件属性:
MVC3 How to check if HttpPostedFileBase is an image
MVC3 如何检查 HttpPostedFileBase 是否是图像
For example ImageApi:
例如ImageApi:
[HttpPost]
[Route("api/image")]
public ActionResult Index(HttpPostedFileBase file)
{
if (file != null && file.ContentLength > 0)
try
{
string path = Path.Combine(Server.MapPath("~/Images"),
Path.GetFileName(file.FileName));
file.SaveAs(path);
ViewBag.Message = "Your message for success";
}
catch (Exception ex)
{
ViewBag.Message = "ERROR:" + ex.Message.ToString();
}
else
{
ViewBag.Message = "Please select file";
}
return View();
}
Hope it help.
希望有帮助。

