asp.net 用FileUpload上傳檔案到檔案夾

來源:互聯網
上載者:User

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
public class Upfield : System.Web.UI.Page
{
public Upfield()
{
}
    public void Upfile(FileUpload Fiup2)
    {
        string savePath = (System.Web.HttpContext.Current.Server.MapPath("..\\..\\FileSave" + "\\" + Session["id"]));
        string[] allowExtensions = { ".doc",".docx",".pptx",".ppt",".xls",".xlsx",".jpg",".jpeg",".gif",".png","pdf",".rar",".zip",".7z",".txt" };
        string fileExtensi = System.IO.Path.GetExtension(Fiup2.FileName).ToLower();
        int a = 0;
        for (int i = 0; i < allowExtensions.Length; i++)
        {
            if (fileExtensi == allowExtensions[i])
            a=1;
        }
        if (!Directory.Exists(savePath))
        {
            Directory.CreateDirectory(savePath);
        }
        else
        {
                if (Fiup2.HasFile)  
                {
                    if (a != 0)
                    {
                        if (Fiup2.PostedFile.ContentLength < 10485760)   
                        {
                            try
                            {
                                string fileName = Fiup2.FileName;
                                HttpPostedFile postFile = Fiup2.PostedFile;
                                string dfileName = Path.GetFileName(Fiup2.FileName).Replace(Path.GetExtension(Fiup2.FileName), " ").Replace("+", " ");
                                string lfileType = fileName.Substring(fileName.LastIndexOf(".") + 1);
                                string time1 = DateTime.Now.ToString().Replace("/", "").Replace(":", "").Replace("-", "").Replace(" ", "");
                                String lfileName = dfileName + time1 + "." + lfileType;
                                Fiup2.SaveAs(System.Web.HttpContext.Current.Server.MapPath("..\\..\\FileSave" + "\\" + Session["id"] + "\\" + lfileName));
                                String fileType = Fiup2.PostedFile.ContentType;
                                int fileSize = Fiup2.PostedFile.ContentLength;
                                String fileMapPath = "..\\..\\FileSave" + "\\" + Session["id"] + "\\" + Fiup2.FileName;    //上傳檔案夾的路徑,自己修改
                                String fileTime = DateTime.Now.ToString();
                                
                                /*Static.setSuZu(fileName, lfileName, fileType, fileSize, fileMapPath, Static.i);  //寫入預存數組
                                Static.i++;  //i++;*/  為多檔案上傳做的標記,可不用
                            }
                            catch (Exception ex)
                            {
                                throw new Exception("上傳發生錯誤!");
                            }
                        }
                        else
                        {
                            throw new Exception("請上傳小於10M的檔案");
                        }
                    }
                    else
                    {
                        throw new Exception("請上傳合適的檔案類型");
                    }
                }
                else
                {
                    throw new Exception("請選擇要上傳的檔案!");
                }
        }
    }

}

這是檔案上傳封裝類,需要時,自己調用,調用方法如下:

protected void BtFadd_Click(object sender, EventArgs e)  //上傳
    {
        try
        {
            Upfield up = new Upfield();
            up.Upfile(Fiup2);
        }
        catch(Exception ex)
        {
            Response.Write("<script language='javascript'>alert('"+ex.Message+"')</script>");
        }
    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.