海滨小城

.net研究

导航

upload 上传多文件

在C#中上传多文件 

protected void Button3_Click(object sender, EventArgs e)
    {
        HttpFileCollection files = HttpContext.Current.Request.Files;
        //状态信息
        System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
        for (int ifile = 0; ifile < files.Count; ifile++)
        {
            HttpPostedFile postedfile = files[ifile];
            string filename, fileExt;
            filename = System.IO.Path.GetFileName(postedfile.FileName);    //获取文件名
            fileExt = System.IO.Path.GetExtension(filename);    //获取文件后缀

            int MaxAllowUploadFileSize = Convert.ToInt32(System.Configuration.ConfigurationSettings.
AppSettings["MaxAllowUploadFileSize"]);     //定义允许上传文件大小
            if (MaxAllowUploadFileSize == 0) { MaxAllowUploadFileSize = 26500; }
            string allowexts = System.Configuration.ConfigurationSettings.AppSettings["AllowUploadFileType"];
            //定义允许上传文件类型
            if (allowexts == "") { allowexts = "doc|docx|rar|xls|xlsx|txt"; }
            Regex allowext = new Regex(allowexts);

            if (postedfile.ContentLength < MaxAllowUploadFileSize && allowext.IsMatch(fileExt)) //检查文件大小及扩展名
            {
                postedfile.SaveAs(Server.MapPath("upload\\" + filename + fileExt));    //upload为与本页面同一目录,可自行修改

                Label1.Text = "客户端路径:" + FileUpload1.PostedFile.FileName + "<br>" +
                          "文件名:" + System.IO.Path.GetFileName(FileUpload1.FileName) + "<br>" +
                          "文件扩展名:" + System.IO.Path.GetExtension(FileUpload1.FileName) + "<br>" +
                          "文件大小:" + FileUpload1.PostedFile.ContentLength + " KB<br>" +
                          "文件MIME类型:" + FileUpload1.PostedFile.ContentType + "<br>" +
                          "保存路径:" + Server.MapPath("upload") + "\\" + FileUpload1.FileName;
            }
            else
            {
                Response.Write("<script>alert('不允许上传类型" + fileExt + "或文件过大')</script>");
            }
        }
    }

在web.config 中增加

  <appSettings>
    <add key="MaxAllowUploadFileSize" value="256000" />
    <add key="AllowUploadFileType" value="doc|docx|rar|xls|xlsx|txt" />
  </appSettings>

记录一下filename 相关的属性

 

if (File1.PostedFile != null) {

//获取上传文件类型//
string FileType=File1.PostedFile.ContentType.ToString();


//检查是否允许上传,当前只允许上传jpg和gif格式文件//
if (FileType=="image/pjpeg" || FileType=="image/gif")
{
//获取文件名//
string FileName=File1.PostedFile.FileName.ToString();   

//获取扩展名//
string FileExt =FileName.Substring(FileName.LastIndexOf("."));

//重新设置一个文件名//
DateTime now = DateTime.Now;
string FrontFileName=now.ToFileTimeUtc()+File1.PostedFile.ContentLength.ToString();
FileName="/Upload/"+FrontFileName+FileExt;

try {
              //文件保存
           File1.PostedFile.SaveAs(Server.MapPath(FileName));
          Response.Write("上传成功");

     }
     catch (Exception exc) {
           Response.Write("上传错误,原因:+"exc.ToString()"");
          }

    }
}

<form enctype="multipart/form-data" runat="server">
选择要上传得文件<input id="File1" type=file runat="server">
<input type=button id="Button1" value="点击开始上传" OnServerClick="Button1_Click" runat="server">
</form>

 

 

 

 

 

 

posted on 2010-08-31 17:08  海滨小城  阅读(260)  评论(0编辑  收藏  举报