【问题标题】:Webmatrix helper : webimage can not upload image fileWebmatrix 助手:webimage 无法上传图像文件
【发布时间】:2011-04-11 09:15:21
【问题描述】:

我刚开始学习webmatrix,安装了helpers library 1.1。 webgrid 和 chart 都可以正常使用。但是使用 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> 

【问题讨论】:

  • “它不能工作”是什么意思?你有错误吗?

标签: file-upload webmatrix


【解决方案1】:

此代码示例建立在 ASP.NET 网站的Working with Images 章节中解释的先前代码示例的基础上。

您需要将“图像”文件夹添加到 WebMatrix Web 应用程序的根目录。您可能还想查看Working with Files 章节。本章介绍如何上传一个或多个文件。

我希望这会有所帮助。

埃里克

【讨论】:

    猜你喜欢
    • 2013-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多