【发布时间】:2016-03-28 12:59:42
【问题描述】:
我看到了这个链接http://forums.asp.net/t/1976497.aspx?ASP%20Net%20MVC%205%20Upload%20Image%20Save%20to%20Database%20Create%20Thumbnail%20and%20Display%20in%20View,这是一个很好的例子,但它不适用于 Asp.Net Mvc 6,因为 Mvc 6 中不存在 HttpPostedFileBase。有人帮我这个 Mvc 6 的代码吗?
// 控制器代码
public IActionResult Create(PublisherInfos publisherInfos, IFormFile file)
{
if (ModelState.IsValid)
{
//attach the uploaded image to the object before saving to Database
file = Request.Form.Files.GetFile("CoverImage");
//get posted file from web form
var filename = publisherInfos.FileName;
var filePathOriginal = _appEnv.ApplicationBasePath + "\\wwwroot\\images";
string savedFileName = Path.Combine(filePathOriginal, filename);
file.SaveAs(savedFileName);
//initialize file upload class to save file to wwwroot directory
FormUpload upload = new FormUpload();
//get the uploaded file name
string Photo = upload.SaveFile(file);
if (Photo != "")
{
Photo = Url.Content($"~/upload/{Photo}");
}
// Add file size and file name into Database
_context.PublisherInfos.Add(publisherInfos);
_context.SaveChanges();
return RedirectToAction("Index", new { Message = PublisherInfoMessageId.DataloadSuccess });
}
return View(publisherInfos);
}
// 那么,类模型如下: // 表单上传类
public class FormUpload
{
private static string UploadDestination { get; set; }
private static string[] AllowedExtensions { get; set; }
IApplicationEnvironment _appEnv;
public FormUpload()
{
//upload config
FormUpload.AllowedExtensions = new string[] { ".jpg", ".png", ".gif" };
FormUpload.UploadDestination = _appEnv.ApplicationBasePath + "\\wwwroot\\images";
}
private bool VerifyFileExtension(string path)
{
return FormUpload.AllowedExtensions.Contains(Path.GetExtension(path));
}
private bool VerifyFileSize(IFormFile file)
{
Double fileSize = 0;
using (var reader = file.OpenReadStream())
{
//get filesize in kb
fileSize = (reader.Length / 1024);
}
//filesize less than 1MB => true, else => false
return (fileSize < 1024) ? true : false;
}
public string SaveFile(IFormFile file)
{
string Filename = "";
if (file.ContentDisposition != null)
{
//parse uploaded file
var parsedContentDisposition = ContentDispositionHeaderValue.Parse(file.ContentDisposition);
Filename = parsedContentDisposition.FileName.Trim('"');
string uploadPath = FormUpload.UploadDestination + Filename;
//check extension
bool extension = this.VerifyFileExtension(uploadPath);
if (extension == false)
{
return "";
}
//check file size
bool filesize = this.VerifyFileSize(file);
if (filesize == false)
{
return "";
}
//save the file to upload destination
file.SaveAs(uploadPath);
}
return Filename;
}
}
// PublisherInfos 模型
public class PublisherInfos
{
[Key]
[Required]
public int PubInfoId { get; set; }
[Column(TypeName = "int")]
[Display(Name = "Image Size")]
public int ImageSize { get; set; }
[StringLength(int.MaxValue, MinimumLength = 8)]
[Column(TypeName = "nvarchar(Max)")]
[DataType(DataType.Text)]
[Display(Name = "Image Filename")]
public string FileName { get; set; }
[Column(TypeName = "Image")]
[Display(Name = "Book Cover")]
public byte[] CoverImage { get; set; }
}
// 发布者信息表单的创建
<form asp-action="Create">
<div class="form-horizontal">
<h4>PublisherInfos</h4>
<hr />
<div asp-validation-summary="ValidationSummary.ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="PubId" class="col-md-2 control-label"></label>
<div class="col-md-10">
<select asp-for="PubId" asp-items="@ViewBag.Publishers" class="form-control"></select>
</div>
</div>
<div class="form-group">
<label asp-for="CoverImage" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input name="CoverImage" class="form-control" />
<input type="file" class="" />
</div>
</div>
<div class="form-group">
<label asp-for="ImageSize" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="ImageSize" class="form-control" />
<span asp-validation-for="ImageSize" class="text-danger" />
</div>
</div>
<div class="form-group">
<label asp-for="FileName" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="FileName" class="form-control" />
<span asp-validation-for="FileName" class="text-danger" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</div>
</div>
</form>
// -------------------------------------- 上面的代码,我为发布者信息表单工作,将图像存储在 CoverImage 字段中,文件大小添加到 ImageSize 字段中,文件名存储在 FileName 字段中。但我无法得到我想存储在 Asp.Net Mvc 6 数据库中的结果。 1. 这段代码有什么问题? 2. 你能帮我找出错误吗? 谢谢,
【问题讨论】:
标签: c# asp.net asp.net-mvc