• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ASP.NETFileUpload上传图片

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

HTML:

<table style="width: 100%">

        <tr>

            <td>

                <asp:ValidationSummary ID="ValidationSummary1" runat="server" />

                <br />

                <asp:FileUpload ID="FileUpload1" runat="server" />

                 <asp:Button ID="btn_upload" runat="server" OnClick="btn_upload_Click" 

                    Text="Upload" />

                  

                <asp:CustomValidator ID="CustomValidator1" runat="server" 

                    ControlToValidate="FileUpload1" Display="Static" 

                    ErrorMessage="You should only can upload image file such as files with .jpg or gif extension" 

                    OnServerValidate="Image_validate">*</asp:CustomValidator>

            </td>

        </tr>

    </table>

code:

  
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing;


public partial class practice_FileUpload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btn_upload_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            string path = @Page.MapPath("User_Edit.aspx").Replace("User_Edit.aspx", "") + "Documents\\";
            string s = path + Session["UserName"].ToString();
            if (!System.IO.Directory.Exists(path + Session["UserName"].ToString()))
            {

                System.IO.Directory.CreateDirectory(path + Session["UserName"].ToString());

            }


            if (FileUpload1.HasFile)
            {


                FileUpload1.SaveAs(Server.MapPath("~/Seeker/Documents/" + Session["UserName"].ToString() + "/" + this.FileUpload1.FileName));
              

            }
        }
    }


    protected void Image_validate(object source, ServerValidateEventArgs args)
    {

        string fileExt = Path.GetExtension(FileUpload1.FileName).ToLower();
        string fileName = Path.GetFileName(FileUpload1.FileName);
      

        if (fileExt != ".jpg" && fileExt != ".gif")
        {
            args.IsValid = false;
          

        }
    }


    protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
    {
        Bitmap bmIP = new Bitmap(FileUpload1.PostedFile.InputStream);

        if (bmIP.Width > 100 | bmIP.Height > 100)
        {

            args.IsValid = false;

        }

        else
        {

            args.IsValid = true;

        }
    }
}

The default size of files uploaded by the FileUpload control is 4MB. This solution was found from the Internet。

值得注意的是,FileUpload 默认上传文件最大为4MB。

如果要增加,则可以在Machine.config里面进行修改
XML:
<httpRuntime
executionTimeout = "110" [in Seconds][number
maxRequestLength = "4096" [number]
requestLengthDiskThreshold = "80" [number]
useFullyQualifiedRedirectUrl = "false" [true|false]
minFreeThreads = "8" [number]
minLocalRequestFreeThreads = "4" [number]
appRequestQueueLimit = "5000" [number]
enableKernelOutputCache = "true" [true|false]
enableVersionHeader = "true" [true|false]
apartmentThreading = "false" [true|false]
requireRootedSaveAsPath = "true" [true|false]
enable = "true" [true|false]
sendCacheControlHeader = "true" [true|false]
shutdownTimeout = "90" [in Seconds][number]
delayNotificationTimeout = "5" [in Seconds][number]
waitChangeNotification = "0" [number]
maxWaitChangeNotification = "0" [number]
enableHeaderChecking = "true" [true|false]
/>


 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap