[英]File Upload in ASP.NET MVC 5
I am unable to upload file in folder. 我无法上传文件夹中的文件。 I am not able to find the mistake.
我无法找到错误。 The UploadFile View returns on same view after uploading file.
UploadFile View在上传文件后返回相同的视图。
Model Class: 型号类:
public class Upload
{
public int UploadId { get; set; }
public string UploadTitle { get; set; }
public string UploadURL { get; set; }
}
Here is the Controller(FileUpload) Action: 这是Controller(FileUpload)动作:
public ActionResult UploadFile(HttpPostedFileBase file, Upload upload)
{
if (ModelState.IsValid)
{
if (file != null)
{
string fil = System.IO.Path.GetFileName(file.FileName);
string path = System.IO.Path.Combine(Server.MapPath("/Content/Uploads/Files"), fil);
file.SaveAs(path);
upload.UploadURL = "/Content/Uploads/Files/" + file.FileName;
}
db.Uploads.Add(upload);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(upload);
}
In my View: 在我看来:
@using (Html.BeginForm("UploadFile, "FileUpload", FormMethod.Post, new { enctype = "multipart/Form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<div class="control-label col-md-2">
<label for="file">Upload Image for Slide:</label>
</div>
<div class="col-md-10">
<input type="file" name="file" id="file" style="width:50%" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
Hi I have tried your same code its works for me. 嗨,我已经尝试了相同的代码,它适用于我。
Controller 调节器
[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase file)
{
if (ModelState.IsValid)
{
if (file != null)
{
string fil = System.IO.Path.GetFileName(file.FileName);
string path = System.IO.Path.Combine(Server.MapPath("/Content/Uploads/Files"), fil);
file.SaveAs(path);
}
return RedirectToAction("Index");
}
return View("UploadFile");
}
View 视图
@using (Html.BeginForm("UploadFile", "Home", FormMethod.Post, new { enctype = "multipart/Form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<div class="control-label col-md-2">
<label for="file">Upload Image for Slide:</label>
</div>
<div class="col-md-10">
<input type="file" name="file" id="file" style="width:50%" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
I have found small mistake in you code in Html.BeginForm in action name "
(double quotes is missing) 我在你的代码中找到了Html.BeginForm代码中的小错误
"
(双引号丢失)
I forgot to mention the required field on UploadURL in above model class: 我忘了在上面的模型类中提到UploadURL上的必填字段:
public class Upload
{
public int UploadId { get; set; }
public string UploadTitle { get; set; }
[Required]
public string UploadURL { get; set; }
}
Required Field validation on UploadURL field restricted the file upload here. UploadURL字段上的必填字段验证限制了此处的文件上载。 I removed the Required field validation from the field.
我从字段中删除了必填字段验证。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.