zoukankan      html  css  js  c++  java
  • dhl: ASP.NET MVC1.0 的图片(文件)上传功能

    知识点:System.Web(System.Web.Abstractions.dll) 的成员HttpPostedFileBase类

    实例1单文件上传:

    前台 :

    <% using (Html.BeginForm("upload", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" }))
       {%>
    <input type="file" name="upfile" />
    <input type ="submit" name ="upload" value ="上传" />
    <%} %>

    后台 :

            [AcceptVerbs(HttpVerbs.Post)]
            public ActionResult upload(HttpPostedFileBase upfile) 
            { 
                if (upfile != null) 
                { 
                    if (upfile.ContentLength > 0) 
                    { 
                        upfile.SaveAs("d:\\7.jpg"); 
                    } 
                }
            return Content(upfile.FileName); 
            } 
    
    

    实例2多文件上传:

    前台 :

    <form action="<%=Url.Action("upload2") %>" enctype="multipart/form-data" method="post">
    <input name="up1" type="file" />
    <input name="up2" type="file" />
    <input type="submit" />
    </form>

    后台:

    public ActionResult upload2() 
            {
                try
                {
                    HttpFileCollectionBase files = Request.Files;
    
                    for (int iFile = 0; iFile < files.Count; iFile++)
                    {
                        HttpPostedFileBase postedFile = files[iFile];
                        string fileName = System.IO.Path.GetFileName(postedFile.FileName);
                        if (!string.IsNullOrEmpty(fileName))
                        {
                            postedFile.SaveAs("d:\\" + fileName);
                        }
                    }
                    TempData["info"] = "成功";
                    return RedirectToAction("Warn"); 
    
                }
                catch (Exception ex)
                {
                    TempData["info"] = "Err" + ex;
                    return RedirectToAction("Warn"); 
                }
    
            }
    
    

    注意:

    多文件上传中,获得图片只能用:for - Requst.File[""]

    而不能用以下foreach() 我也不知道为什么。(可能是HttpFileCollectionBase不是HttpPostedFileBase的集合,HttpFileCollectionBase..::.Item 屬性 是(String)类型,参考:http://msdn.microsoft.com/zh-tw/library/cc680819.aspx

    HttpFileCollectionBase files = Request.Files;

    foreach (HttpPostedFileBase fileBase in files)
                    {
                        fileBase.SaveAs("d:\\a.jpg");
                    }

    所以用Foreach的话就这样写:

                    foreach (string fileBase in files)
                    {

                        HttpPostedFileBase postedFile = files[fileBase];
                        string fileName = System.IO.Path.GetFileName(postedFile.FileName);
                        if (!string.IsNullOrEmpty(fileName))
                        {
                            postedFile.SaveAs("d:\\" + fileName);
                        }
                    }

  • 相关阅读:
    Getting Started with LINQ in C# 章节概况
    LA 2572 Viva Confetti (Geometry.Circle)
    uva 10652 Board Wrapping (Convex Hull, Easy)
    poj 2743 && LA 3403 Mobile Computing (mideasy Search)
    poj 3525 Most Distant Point from the Sea (DC2 + Half Plane)
    poj 3134 && LA 3621 Power Calculus (迭代加深深度优先搜索)
    LA 4728 Squares (二维凸包+旋转卡壳)
    uva 10256 The Great Divide (Convex Hull, Simple)
    hdu 2454 Degree Sequence of Graph G
    poj 1041 John's trip (Euler Circuit)
  • 原文地址:https://www.cnblogs.com/dudu837/p/1621094.html
Copyright © 2011-2022 走看看