2016-03-28 13 views
0

このリンクを見ましたhttp://forums.asp.net/t/1976497.aspx?ASP%20Net%20MVC%205%20Upload%20Image%20Save%20to%20Database%20Create%20Thumbnail%20and%20Display%20in%20View、これは良い例ですが、HttpPostedFileBaseがMvcに存在しないため、Asp.Net Mvc 6には適用されません6.次のように誰かがMVC 6のため、このコードAsp.net Mvc 6ブラウズ画像を結合して画像サイズの画像名を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フィールドに保存し、ファイルサイズを追加しましたFileSizeフィールドに格納されているImageSizeフィールドとファイル名に追加します。しかし、私はAsp.Net Mvc 6のデータベースに保存したい結果を得ることができません。 1.このコードの何が間違っていますか? 2.間違いを見つけるのを助けてくれますか? は

答えて

0

使用 "IFormFile" HttpPostedFileBase

の代わりに、ありがとう