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>");
    } 我自己写的源代码你自己慢慢研究吧

  • 相关阅读:
    使用VS2013直接升级到VS2019,Cmake编译CCC4.0版本错误记录
    最近使用LuaSqlite3时,不得不说的——事务提交ATTACH语句的巨坑
    LuaSocket学习之tcp服务端
    LuaSocket HTTP 初识记录
    LuaRocks安装教程
    Lua5.1-----函数可变参数详解
    DataTable与json互转,字段类型信息丢失问题初探
    json数组与对象数组
    asp.net中封装路由信息的对象--RouteData源码
    人类的心理行为模式(几个心理学实验)
  • 原文地址:https://www.cnblogs.com/zhangzhifeng/p/3663612.html
Copyright © 2011-2022 走看看