Asp.net mvc 3 MVC3图像上传库

Asp.net mvc 3 MVC3图像上传库,asp.net-mvc-3,image-uploading,Asp.net Mvc 3,Image Uploading,我已经使用ASP.NET Mvc 3和Microsoft.Web.Helpers NuGet包实现了图像文件上传。实现非常简单,因为它允许您浏览文件并将其上载到指定目录 以下是我使用ASP.NET MVC 3和Microsoft.Web.HelpersNuGet插件的图像上传解决方案 现在显示视图模型代码 namespace MvcImageUpload.Models { public class ImageUploadViewModel { [UIHint(&quo

我已经使用ASP.NET Mvc 3Microsoft.Web.Helpers NuGet包实现了图像文件上传。实现非常简单,因为它允许您浏览文件并将其上载到指定目录

以下是我使用ASP.NET MVC 3和Microsoft.Web.HelpersNuGet插件的图像上传解决方案

现在显示视图模型代码

 namespace MvcImageUpload.Models {
    public class ImageUploadViewModel {
        [UIHint("UploadedImage")]
        public string ImageUrl { get; set; }
        public string ImageAltText { get; set; }
    }    
}
现在对于控制器我只是把它放到了主控制器中,因为这只是一个让它工作的模拟项目。我刚刚添加了一个
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-form方法,并将编码类型设置为“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 />
    }
@model MvcImageUpload.Models.ImageUploadViewModel
@使用Microsoft.Web.Helpers;
@{
ViewBag.Title=“Index”;
}
图像上传器
@使用(Html.BeginForm(“上传”、“主页”、FormMethod.Post、,
新的{@encType=“multipart/form data”}){
@FileUpload.GetHtml(initialNumberOfFiles:1,allowmorefilestobeaded:false,
includeFormTag:false,addText:“添加文件”,uploadText:“上载文件”)
@DisplayFor(x=>x,“ImageViewer”)
}
下面是自定义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"/>
}
@model MvcImageUpload.Models.ImageUploadViewModel
@如果(Model!=null){
上传成功!

Alt Text已设置为@Model.ImageAltText

}
这一切都起作用了,图像成功上传到表单post上的/Uploads/Images/FileName.extension

我的问题 我现在如何能有另一个视图来显示该目录中的所有图像(已分页),并能够从视图和目录中选择和删除图像


另外,我知道Microsoft.Web.Helpers.FileUpload支持上载多个文件,但我找不到如何在当前解决方案中实现这一点。任何帮助都将不胜感激。

您所问的问题在我看来比任何查询都更重要

显示: 通过DirectoryInfo从Uploads/images目录获取所有图像。。。您可以根据某个扩展名搜索目录,然后它将为您提供一个结果集,您可以对该结果集进行迭代

创建一个将所有记录显示为图像链接的视图,并在控制器中将结果集提取到该视图。。。。按您希望在视图中显示的方式绑定这些记录

System.IO.DirectoryInfo info = new System.IO.DirectoryInfo("your directory path");
               var filesinfo= info.GetFiles("*.jpg", System.IO.SearchOption.AllDirectories);
               var filenum= filesinfo.GetEnumerator();
               while (filenum.MoveNext())
               {
                   //populate some entity like in your case you have ImageUploadViewModel
               }
您可以使用Ajax实现删除逻辑,也可以通过post back实现删除逻辑,这取决于您想要它的方式

遵循本教程,它将让您了解


但是,您要问的更像是实现代码,而不是任何问题……

我之前采用的方法是将文件信息持久化到数据库中(或任何适当的方法)。e、 g.路径、文件名、内容类型、文件大小。 这使您在编辑(alt文本、标题、说明、与其他对象的关系)时具有最大的灵活性。 然后可以根据路径约定处理下载/查看文件,方法是创建一个ViewImage控制器,该控制器仅获取一个图像id作为参数。 然后,您可以从文件路径构建url,只需设置内容类型。
IIS然后执行其余操作。

单击上载图像按钮后,系统应调用使用
请求
获取文件的方法

    [HttpPost]
    public ActionResult Upload()
    {
        if(Request.Files != null && Request.Files.Count > 0)
        {
                for (int i = 0; i < request.Files.Count; i++)
                {
                    var postFile = request.Files[i];
                    if (postFile != null && postFile.ContentLength > 0)
                    {
                        if (postFile.ContentLength < GetMaxRequestLength())  //10MB
                        {
                             var file = new ContractAttachment
                            {
                                Name = Path.GetFileName(postFile.FileName),
                                ContentType = postFile.ContentType,
                                FileLength = postFile.ContentLength,
                                FileData = GetStreamBuffer(postFile)
                            };
                            files.Add(file);
                        }

                    }
                }
          }

    }
[HttpPost]
公共行动结果上载()
{
如果(Request.Files!=null&&Request.Files.Count>0)
{
对于(int i=0;i0)
{
if(postFile.ContentLength

希望这有帮助。

问题是我不确定如何实施。我发布这篇文章是为了帮助其他可能需要知道如何使用mvc 3上传文件的人。我已经更新了我的答案,并添加了一些基本教程的链接,这些教程将帮助您创建此。。。。