简单图片上传示例

前台页面:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>简单图片上传示例</title>
    <script type="text/javascript">
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传" />
        <br />
        <asp:Image ID="Image1" runat="server" />
    </div>
    </form>
</body>
</html>

后台代码:

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace 上传文件Demo
{
    public partial class aspnet上传图片 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (FileUpload1.HasFile)
                {
                    //文件名
                    string fileName = FileUpload1.FileName;
                    //文件名(完全限定名:包含所在路径)
                    //string filePath = FileUpload1.PostedFile.FileName;

                    //文件类型
                    string fileType = FileUpload1.PostedFile.ContentType;
                    string[] arrFileType = new string[]{
                        "image/jpeg",
                        "image/jpeg",
                        "image/jpeg",
                        "image/bmp",
                        "image/gif",
                        "image/png"
                    };
                    if (!arrFileType.Contains(fileType))
                    {
                        Label1.Text = "请选择图片类型的文件!";
                        return;
                    }

                    //文件大小以字节为单位
                    int fileLength = FileUpload1.PostedFile.ContentLength;
                    if (fileLength>1048576)
                    {
                        Label1.Text = "请选择小于1M的文件";
                        return;
                    }

                    //要在服务器上存储的名字
                    string newName = Guid.NewGuid().ToString();

                    //获取文件扩展名
                    string[] arr = fileName.Split('.');
                    string fType = newName + "." + arr[arr.Length - 1];

                    string serverPath = Server.MapPath("~/Images/") + fType;
                    FileUpload1.SaveAs(serverPath);
                    Image1.ImageUrl = "~/Images/" + fType;
                }
                else
                {
                    Label1.Text = "请选择上传的文件";
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
    }
}

由于asp.net默认配置是限制了上传文件的大小为4M,超出时会报错.所以要自己做文件大小限定,要先修改web.config文件里

的上传文件大小限制,修改文件如下:

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <!--修改上传文件的最大限制-->
    <httpRuntime maxRequestLength="40960" executionTimeout="3600" />
  </system.web>
</configuration>
posted @ 2012-08-05 00:12  ``炯``  阅读(401)  评论(0编辑  收藏  举报