好的,这是我的观点
@using (Html.BeginForm("Upload", "Pictures", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div>
Title:<br/>
@Html.EditorFor(x => x.Title)<br/>
@Html.ValidationMessageFor(x => x.Title)<br/>
@Html.TextBoxFor(x => x.File, new {
type = "file"
})<br/>
@Html.ValidationMessageFor(x => x.File)<br/>
Description:<br/>
@Html.TextAreaFor(x => x.Description)<br/>
@Html.ValidationMessageFor(x => x.Description)
</div>
<div style="clear:both"></div>
<p><input type="submit" value="Save"/></p>
}
这是我的视图模型
public class UploadModel
{
[Required(ErrorMessage=("You have not selected a file"))]
public HttpPostedFileBase File { get; set; }
[Required(ErrorMessage = "Please enter a title")]
[StringLength(50)]
public string Title { get; set; }
[StringLength(400)]
public string Description { get; set; }
}
这是我的控制器动作。
[Authorize(Roles = "Approved")]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Upload(UploadModel m)
{
byte[] uploadedFile = null;
Byte123 xxx = new Byte123();
if (m.File != null && !string.IsNullOrEmpty(m.Title))
{
//var fileName = System.IO.Path.GetFileName(m.File.FileName);
//string c = m.File.FileName.Substring(m.File.FileName.LastIndexOf("."));
// m.Title = m.Title.Replace(c, "");
uploadedFile = new byte[m.File.InputStream.Length]; //you get the image as byte here but you can also save it to file.
这是 MVC 代码。如果您使用的是 Web 表单,那么代码应该更短。我从一个链接中得到了这个,但现在找不到它,所以只发布了我自己的代码。您还需要确保使用 Cpanel 在您的主机中启用了写入权限。