• .net简单的fileupload控件上传


    前台代码:

    <asp:FileUpload ID="FileUpload1" runat="server" />
    <asp:Button ID="filebut" runat="server" Text="上传" onclick="filebut_Click" />

    后台代码:

    protected void filebut_Click(object sender, EventArgs e)
    {
    if (FileUpload1.PostedFile.FileName == "")
    {
    Response.Write("<script>alert('请选择上传文件!')</script>");
    }
    else
    {

    HttpFileCollection uploadfiles = Request.Files;
    string filetiem = DateTime.Now.ToString();//当前时间
    string fileusername = Session.Contents["userid"].ToString();//当前用户
    string gongwid = Request.QueryString["id"];//文章id

    for (int i = 0; i < uploadfiles.Count; i++)
    {
    HttpPostedFile mypost = uploadfiles[i];
    try
    {
    string filepath = mypost.FileName;
    string filename = filepath.Substring(filepath.LastIndexOf("\") + 1);//获取文件名
    string serverpath = Server.MapPath("UploadFile\") + filename;//服务器上传地址
    if (System.IO.File.Exists(serverpath))
    {
    Response.Write("<script>alert('文件已存在请重新命名!')</script>");
    }
    else
    {
    mypost.SaveAs(serverpath);
    string filestrsql = "insert into OA_FILE(wjlj,wjm,gongwid,scr,scsj)values('" + serverpath + "','" + filepath + "','" + gongwid + "','" + fileusername + "','" + filetiem + "')";//sql执行语句
    int sfcg = SqlHelper.wjsc(filestrsql);
    if (sfcg > 0)
    {
    labzt.Text = "文件" + i + "上传成功!路径:" + serverpath + " ";
    }
    }

    }
    catch (System.Exception ex)
    {
    labzt.Text = "上传错误!原因:" + ex.Message.ToString();
    }
    }
    }

    }

    由于先前是准备多加价格fieldupload控件的,但是不是特别美观,但是实现了上传的功能。

  • 相关阅读:
    架设WCF项目出现的问题
    百思不得其解的"Failed to allocate a managed memory buffer of 268435456 bytes."错误解决
    Ajax 分页
    关于Asp.net调用外部程序的拒绝访问错误
    转贴:[翻译]Visual Studio 2008 Code Metrics
    荀子,劝学篇(部分)
    .net设计模式(转载)
    人月神话读书笔记
    Memory food
    2010年4月12日,今天做计划
  • 原文地址:https://www.cnblogs.com/cjdonet/p/5760560.html
Copyright © 2020-2023  润新知