2016-03-28 49 views
0

我看到這個鏈接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,因爲HttpPostedFileBase不存在於Mvc中6.是否有人幫我這個代碼MVC的6Asp.net Mvc 6 Combine瀏覽圖片並將圖片大小圖片名稱添加到sql服務器

//控制器代碼

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); 
    } 

//然後,類模型如下: // FormUpload類

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.你能幫我找到錯誤嗎? 謝謝

回答

0

在HttpPostedFileBase的地方使用「IFormFile」