Webmatrix helper : webimage can not upload image file
I just start to learn the webmatrix, and installed the helpers library 1.1. the webgrid and chart are all normal to use .but when use webimage to upload a image file. it can not work. the code is :
@{
WebImage photo = null;
var newFileName = "";
var imagePath = "";
if(IsPost){
photo = WebImage.GetImageFromRequest();
if(photo != null){
newFileName =开发者_JS百科 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>
This code example builds on the previous code example explained in the Working with Images chapter of the ASP.NET web site.
You need an "images" folder added to the root of your WebMatrix web application. You may also want to look at the Working with Files chapter. This chapter shows how to upload one or more files.
I hope this helps.
Erik
精彩评论