2016-10-24 12 views
-2

だから私はこの小さなコードをすべて検索し、ほとんどすべての作業を完了しました。私は画像をアップロードし、作成したフォルダに保存することができます。他の値もデータベースに保存することができます。今私の問題は、私は別のページに画像を表示するために呼び出すことができるように、データベースにファイルパスを保存したいということです。何らかの理由で、アップロードされているイメージの名前だけが保存され、パスは保存されません。データベースへのファイルパスを保存すると、ファイル名のみが保存されます

私はそれをデバッグすると私は評価されている file.SaveAs(pathLoc);

式に時計を追加し、何の価値ボイドここ

を持っていないとき、すべてが渡さなっているものを参照してくださいコードIであります使用beed」VEの

コントローラ

[HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Create(ImageInfo info, HttpPostedFileBase file) 
    { 
     if (ModelState.IsValid) 
     { 
      if (file != null) 
      { 
       var pic = Path.GetFileName(file.FileName); 
       var pathLoc = Path.Combine(Server.MapPath("~/Uploads/") + pic); 
       file.SaveAs(pathLoc); 


       info.ImagePath = file.FileName; 
       db.SaveChanges(); 
      } 

      return RedirectToAction("Index"); 
     } 

     return View(info); 
    } 

ビュー

<h2>Create</h2> 


@using (Html.BeginForm("Create", "ImageInfoes", null, FormMethod.Post,  new { enctype = "multipart/form-data" })) 
{ 
    @Html.AntiForgeryToken() 
    @Html.ValidationSummary(true) 

<div class="form-horizontal"> 
    <h4>ImageInfo</h4> 
    <hr /> 
    @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 

    <div class="form-group"> 
     @Html.LabelFor(model => model.ImageName, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.ImageName, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.ImageName, "", new { @class = "text-danger" }) 
     </div> 
    </div> 
    <div class="form-group"> 
     @Html.LabelFor(model => model.ImageSize, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      @Html.EditorFor(model => model.ImageSize, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.ImageSize, "", new { @class = "text-danger" }) 
     </div> 
    </div> 
    <div class="form-group"> 
     @Html.LabelFor(model => model.ImagePath, htmlAttributes: new { @class = "control-label col-md-2" }) 
     <div class="col-md-10"> 
      <input id="ImagePath" title="Upload a product image" type="file" name="file" /> 
     </div> 
    </div> 
    <div class="form-group"> 
     <div class="col-md-offset-2 col-md-10"> 
      <input type="submit" value="Create" class="btn btn-default" /> 
     </div> 
    </div> 
</div> 
} 

    <div> 
     @Html.ActionLink("Back to List", "Index") 
    </div> 

モデル

public partial class ImageInfo 
    { 
     public byte id { get; set; } 
     public string ImageName { get; set; } 
     public Nullable<long> ImageSize { get; set; } 
     public string ImagePath { get; set; } 
    } 

ものはいっぱい助けになります。

+0

info.ImagePath = file.FileName。 => FileNameはあなたにfile ...という名前だけを与えます。 –

+0

'file.Filename'の代わりに' pathLoc'を設定しますか? – Berkay

+0

私はfile.FileNameからpathLocにdb.SaveChanges()を変更します。スローとエラー。 – whisk

答えて

1

完全パスの場合は、ファイルパスとファイル名の両方を結合する必要があります。

Console.WriteLine(Path.Combine(System.Reflection.Assembly.GetExecutingAssembly().Location, System.IO.Path.GetRandomFileName())); 

C:また、以下の

tv1k1uev.jsqプロジェクト\ ConsoleApplication5 \短所\ Visual Studioの 2015 \ \ Users \ユーザー02483695 \ドキュメント oleApplication5 \ binに\デバッグ\ ConsoleApplication5.exe \ファイル、ファイルディレクトリ、ファイルパス、ファイル拡張子のサンプルがあります。結果として

System.IO.FileInfo fileInfo = new FileInfo("filename.file"); 
var fileDirectory = fileInfo.DirectoryName; 
var fileName = fileInfo.Name; 
var fileExtension = fileInfo.Extension; 
var filePathandFileNameBoth = fileInfo.FullName; 

Console.WriteLine("filePathandFileNameBoth: "); 
Console.WriteLine(filePathandFileNameBoth); 
Console.WriteLine("-"); 
Console.WriteLine("fileDirectory:"); 
Console.WriteLine(fileDirectory); 
Console.WriteLine("-"); 
Console.WriteLine("fileName:"); 
Console.WriteLine(fileName); 
Console.WriteLine("-"); 
Console.WriteLine(filePathandFileNameBoth == fileDirectory + "\\" + fileName ? fileExtension : "I'm totally wrong"); 
Console.ReadLine(); 

Console Output
+0

私は 'var pathLoc = Path.Combine(Server.MapPath("〜/ Uploads/")+ pic)で何をしているのですか? – whisk

+1

この "info.ImagePath = file.FileName;"あなたが間違っている場所です。だから、私はすべての "FileInfo"オブジェクトのプロパティについてディレクトリ/パス/フルパスと拡張について説明した。 –

関連する問題