2011-04-11 26 views
2

我剛開始學習webmatrix,並安裝了助手庫1.1。 webgrid和圖表都是正常的使用,但是當使用webimage上傳圖片文件時。它不能工作。代碼:Webmatrix助手:webimage無法上傳圖像文件

@{ 
    WebImage photo = null; 
    var newFileName = ""; 
    var imagePath = ""; 

    if(IsPost){ 
     photo = WebImage.GetImageFromRequest(); 
     if(photo != null){ 
      newFileName = Guid.NewGuid().ToString() + "_" + 
      Path.GetFileName(photo.FileName); 
      imagePath = @"images\" + newFileName; 

      photo.Save(@"~\" + imagePath); 
     } 
    } 
} 
<!DOCTYPE html> 
<html> 
    <head> 
    <title>Image Upload</title> 
    </head> 
    <body> 
    <form action="" method="post" enctype="multipart/form-data"> 
     <fieldset> 
     <legend> Upload Image </legend> 
     <label for="Image">Image</label> 
     <input type="file" name="Image" /> 
     <br/> 
     <input type="submit" value="Upload" /> 
     </fieldset> 
    </form> 
    <h1>Uploaded Image</h1> 
    @if(imagePath != ""){ 
     <div class="result"> 
     <img src="@imagePath" alt="image" /> 
     </div> 
    } 
    </body> 
</html> 
+1

你是什麼意思「它不能工作」?你有錯誤嗎? – 2011-04-11 16:15:00

回答

0

此代碼示例建立在前面的代碼示例中的ASP.NET網站的Working with Images章解釋。

您需要將「images」文件夾添加到WebMatrix Web應用程序的根目錄中。你也可以看看Working with Files這一章。本章介紹如何上傳一個或多個文件。

我希望這會有所幫助。

Erik