Mvc 3 图片上传库
我已经使用 ASP.NET Mvc 3 和 Microsoft.Web.Helpers NuGet 包实现了图像文件上传。实现非常简单,因为它允许您浏览文件并将其上传到指定目录。
以下是我使用 ASP.NET MVC 3 和 Microsoft.Web.Helpers NuGet 插件的图像上传解决方案。
现在是 ViewModel 代码
namespace MvcImageUpload.Models {
public class ImageUploadViewModel {
[UIHint("UploadedImage")]
public string ImageUrl { get; set; }
public string ImageAltText { get; set; }
}
}
现在用于控制器我只是将其放入 Home 控制器中,因为这只是一个让它工作的模拟项目。我刚刚添加了一个 ActionResult
,它将 ImageUploadViewModel 作为参数。
public ActionResult Upload(ImageUploadViewModel model) {
var image = WebImage.GetImageFromRequest();
if (image != null) {
if (image.Width > 500) {
image.Resize(500, ((500 * image.Height) / image.Width));
}
var filename = Path.GetFileName(image.FileName);
image.Save(Path.Combine("../Uploads/Images", filename));
filename = Path.Combine("~/Uploads/Images", filename);
model.ImageUrl = Url.Content(filename);
model.ImageAltText = image.FileName.Substring(0, image.FileName.Length - 4);
}
return View("Index", model);
}
我对图像上传的看法很简单,它有一个 Html.BeginForm,它处理 Post 表单方法,并将编码类型设置为“multipart/form-data”。
然后,使用 Microsoft.Web.Helpers.FileUpload 帮助程序,我从 HTTP post 请求图像,然后使用名为 ImageViewer 的自定义 DisplayFor 模板显示它。
@model MvcImageUpload.Models.ImageUploadViewModel
@using Microsoft.Web.Helpers;
@{
ViewBag.Title = "Index";
}
<h2>Image Uploader</h2>
@using (Html.BeginForm("Upload", "Home", FormMethod.Post,
new { @encType = "multipart/form-data" })) {
@FileUpload.GetHtml(initialNumberOfFiles: 1, allowMoreFilesToBeAdded: false,
includeFormTag: false, addText: "Add Files", uploadText: "Upload File") <br />
<input type="submit" name="submit"
value="Upload Image" text="Upload Images"
style="font-size: .9em;" />
@Html.DisplayFor(x => x, "ImageViewer")<br />
}
这是自定义 DisplayTemplate 的样子
@model MvcImageUpload.Models.ImageUploadViewModel
@if (Model != null) {
<h4 style="color:Green;">Upload Success!</h4>
<p>
Alt Text has been set to <strong>@Model.ImageAltText</strong>
</p>
<img style="padding: 20px;"
src="@(String.IsNullOrEmpty(Model.ImageUrl) ? "" : Model.ImageUrl)"
id="uploadedImage" alt="@Model.ImageAltText"/>
}
这一切正常,图像已成功上传到表单帖子上的 /Uploads/Images/FileName.extension。
我的问题
现在如何才能有另一个视图来显示该目录中的所有图像,分页并能够从视图和目录中选择和删除图像?
我也知道 Microsoft.Web.Helpers.FileUpload 支持上传多个文件,但我找不到如何用我当前的解决方案来实现这一点。任何帮助将不胜感激。
I have implemented a file upload for images using ASP.NET Mvc 3 and the Microsoft.Web.Helpers NuGet package. The implementation is quit simple as it allows you to browse for a file and upload it to a specified directory.
Here is what I have for my image upload solution using ASP.NET MVC 3 and the Microsoft.Web.Helpers NuGet plugin.
Now the ViewModel code
namespace MvcImageUpload.Models {
public class ImageUploadViewModel {
[UIHint("UploadedImage")]
public string ImageUrl { get; set; }
public string ImageAltText { get; set; }
}
}
Now for the controller I've simply dropped this into the Home controller, since this is just a mock project to get it working. I just added an ActionResult
which takes an ImageUploadViewModel as a parameter.
public ActionResult Upload(ImageUploadViewModel model) {
var image = WebImage.GetImageFromRequest();
if (image != null) {
if (image.Width > 500) {
image.Resize(500, ((500 * image.Height) / image.Width));
}
var filename = Path.GetFileName(image.FileName);
image.Save(Path.Combine("../Uploads/Images", filename));
filename = Path.Combine("~/Uploads/Images", filename);
model.ImageUrl = Url.Content(filename);
model.ImageAltText = image.FileName.Substring(0, image.FileName.Length - 4);
}
return View("Index", model);
}
My view for the uploading of images is simple, it has an Html.BeginForm, which handles the Post form method and has the encoding type set to be "multipart/form-data".
Then using The Microsoft.Web.Helpers.FileUpload helper, I request an image from the HTTP post and then display it using a custom DisplayFor template, called ImageViewer.
@model MvcImageUpload.Models.ImageUploadViewModel
@using Microsoft.Web.Helpers;
@{
ViewBag.Title = "Index";
}
<h2>Image Uploader</h2>
@using (Html.BeginForm("Upload", "Home", FormMethod.Post,
new { @encType = "multipart/form-data" })) {
@FileUpload.GetHtml(initialNumberOfFiles: 1, allowMoreFilesToBeAdded: false,
includeFormTag: false, addText: "Add Files", uploadText: "Upload File") <br />
<input type="submit" name="submit"
value="Upload Image" text="Upload Images"
style="font-size: .9em;" />
@Html.DisplayFor(x => x, "ImageViewer")<br />
}
Here is what the custom DisplayTemplate looks like
@model MvcImageUpload.Models.ImageUploadViewModel
@if (Model != null) {
<h4 style="color:Green;">Upload Success!</h4>
<p>
Alt Text has been set to <strong>@Model.ImageAltText</strong>
</p>
<img style="padding: 20px;"
src="@(String.IsNullOrEmpty(Model.ImageUrl) ? "" : Model.ImageUrl)"
id="uploadedImage" alt="@Model.ImageAltText"/>
}
This all works and the image gets successfully uploaded to the /Uploads/Images/FileName.extension on the form post.
My question
How can I now have another view to display all the images in that directory, paged and be able to select and delete and image, from the view and the directory?
Also I know the Microsoft.Web.Helpers.FileUpload, supports uploading of multiple files, but I can't find how to implement this with my current solution. Any help would be greatly appriceated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
单击“上传图像”按钮后,系统应调用使用
Request
的方法来获取文件。希望这有帮助。
After you click the Upload Image button, the system should call method which uses
Request
to get the file.Hope this help.
你所问的问题对我来说看起来更像是实现,然后是任何查询......
显示:
通过 DirectoryInfo 从 Uploads/Images 目录中获取所有图像...您可以根据某些扩展名搜索目录,然后它会给您一个可以迭代的结果集...
创建一个视图,将所有记录显示为图像链接和控制器中将结果集获取到该视图....按照您希望它们在视图中显示的方式绑定这些记录...
并且您可以使用 Ajax 或通过回发来实现删除逻辑,具体取决于您想要的方式.. ..
<一href="http://www.pluralsight-training.net/microsoft/players/PSODPlayer.aspx?author=scott-allen&name=mvc3-building-views&mode=live&clip=0&course=aspdotnet-mvc3 -intro" rel="nofollow">Asp.net MVC Views 遵循本教程,它会让您完成这个....
但您所要求的更像是实施代码没有任何问题......
what you are asking about looks rather implementation to me then any query....
to Display:
Fetch all images from your Uploads/Images directory through DirectoryInfo... you can search a directory based on some extension and then it will give you a result set which you can iterate.....
Create a view that will display all records as Image links and in controller fetch the resultset to that View.... Bind those records as you want them to display in your VIEW...
and you can implement you delete logic using Ajax or through post back depends how you want it....
Asp.net MVC Views following this tutorial and it will let you go through this....
but again what you are asking is more like implementation Code not any issue....
我之前遵循的方法是将文件信息保留在数据库中(或任何适当的方法)。例如路径、文件名、内容类型、文件大小。
这为您在编辑时提供了最大的灵活性(替代文本、标题、描述、与其他对象的关系)。
然后可以根据路径约定来处理下载/查看文件,方法是创建一个 ViewImage 控制器,该控制器仅获取图像 id 作为参数。
然后,您可以从文件的路径构建一个 url,并且只需要设置内容类型。
然后 IIS 会完成剩下的工作。
The approach I've followed previously, is to persist the file information in a database(or whatever is appropriate). e.g. path, filename, content-type, filesize.
This gives you the most flexibility when editing (alt text, title, description, relation to other objects).
Downloading/Viewing the files can then be handled based on path convention, by creating a ViewImage controller which just gets an image id as parameter.
You can then build a url from the path to the file and you only need to set the content-type.
IIS then does the rest.