zoukankan      html  css  js  c++  java
  • ASP.NET网站文件上传下载功能

    if (!IsPostBack)
    {
    if (Application["RaNum"] == null)
    {
    Random ra = new Random();
    Application["RaNum"] = ra;
    }
    }
    if (pictureUP.HasFile)
    {
    //Response.Write(FileUpload1.PostedFile.ContentLength);
    if (pictureUP.PostedFile.ContentLength < 200 * 1024)
    {
    //string strPath = Server.MapPath("~/uploadfiles/");
    //FileUpload1.SaveAs(strPath+FileUpload1.FileName);
    string[] strs = pictureUP.FileName.Split(new char[] { '.' });
    string strExt = strs[strs.Length - 1].ToLower();
    ArrayList list = new ArrayList();
    list.Add("jpg");
    list.Add("gif");
    list.Add("bmp");
    if (list.Contains(strExt))
    {
    ArrayList listType = new ArrayList();
    listType.Add("image/pjpeg");
    // TextBox1.Text=pictureUP.PostedFile.ContentType;
    listType.Add("image/gif");
    listType.Add("image/bmp");
    if (listType.Contains(pictureUP.PostedFile.ContentType))
    {
    Random ra = (Random)Application["RaNum"];
    int strRa = ra.Next();
    Int64 strDate = Convert.ToInt64(DateTime.Now.ToString("yyyyMMddhhmmss"));
    string FileName = strDate.ToString("X") + strRa.ToString("X") + pictureUP.FileName;
    string strPath = Server.MapPath("~/images/user/");
    string strFilePath = strPath + FileName;
    // TextBox1.Text = strFilePath;
    while (File.Exists(strFilePath))
    {
    strRa = ra.Next();
    strDate = Convert.ToInt64(DateTime.Now.ToString("yyyyMMddhhmmss"));
    FileName = strDate.ToString("X") + strRa.ToString("X") + pictureUP.FileName;
    strFilePath = strPath + FileName; }
    try
    {
    pictureUP.SaveAs(strFilePath);
    // Response.Write("上传成功!");
    userInfoUpData U_Info = new userInfoUpData();
    U_Info.UserID = userID.Text;
    U_Info.UserPwd = userPWD.Text;
    U_Info.UserName = userName.Text;
    U_Info.UserSex = userSex.Text;
    U_Info.UserAge = Convert.ToInt32(userAge.Text);
    U_Info.UserTell = userTell.Text;
    U_Info.UserAdr = userAdrr.Text;
    U_Info.UserPic = FileName;
    UserBLL U_Info_Bll = new UserBLL();
    if (U_Info_Bll.UserInfoUp(U_Info) > 0)
    { Response.Write("<script>alert('修改成功')</script>");
    }
    else
    {
    Response.Write("<script>alert('修改失败')</script>");
    }
    }
    catch
    {
    Response.Write("上传失败!");
    }
    }
    }
    else
    {
    Response.Write("<script>alert('上传的文件类型错误!')</script>");
    }
    }
    else
    {
    Response.Write("<script>alert('上传文件的大小超过了200k!')</script>");
    }
    }
    else
    {
    Response.Write("<script>alert('请选择一个上传的文件!')</script>");
    } 我自己写的源代码你自己慢慢研究吧

  • 相关阅读:
    c# 根据当前时间获取,本周,本月,本季度,月初,月末,各个时间段
    Button 对 TreeView1 所有节点的全选
    启动Myeclipse报错“Failed to create the Java Virtual Machine”的解决办法
    Myeclipse 启动报错 Failed to create the java Virtual Machine
    ng-cordova和cordova区别
    Java高效计数器
    WebView 简介
    Hibernate——(4)Hibernate映射类型
    Hibernate——(3)主键生成方式
    Hibernate——(2)增删改查
  • 原文地址:https://www.cnblogs.com/zhangzhifeng/p/3663612.html
Copyright © 2011-2022 走看看