C# 選擇本機檔案並上傳

參考自:
http://www.dotblogs.com.tw/puma/archive/2008/11/07/5910.aspx
http://www.codeproject.com/Articles/19398/Example-for-FolderBrowserDialog-in-C
http://www.dotblogs.com.tw/mis2000lab/archive/2011/09/26/fileupload_serverpath_2011.aspx

選擇本機檔案的兩種方式:
1.利用FolderBrowserDialog或OpenFileDialog

<asp:TextBox ID="txtdfolder" runat="server"></asp:TextBox>
<asp:Button ID="ButtonSelectFolder" runat="server" OnClick="btnSelectFolder_Click" text="select folder" />
<asp:TextBox ID="txtdfile" runat="server"></asp:TextBox>
<asp:Button ID="ButtonSelectFile" runat="server" OnClick="btnSelectFile_Click" text="select file" />

 

public partial class SelectFolder : Form
{
    public SelectFolder()
    {
        InitializeComponent();
    }
    private void btnSelectFolder_Click(object sender, EventArgs e)
    {
        FolderBrowserDialog path = new FolderBrowserDialog();
        // Basically, there are two additional settings available to make the dialog more customized. First, the property
        // ShowNewFolderButton determines whether the user can create a new folder or not.
        this.path.ShowNewFolderButton = false;
        // Second, the property RootFolder defines the top level folder of the dialog, i.e. the folder which will be shown initially.
        this.folderBrowserDialog.RootFolder = System.Environment.SpecialFolder.MyComputer;
        DialogResult result=path.ShowDialog();
        if (result==DialogResult.OK)
        {
            // the code here will be executed if the user presses Open in the dialog.
            this.txtPath.Text = path.SelectedPath;
        }
    }
    
    private void btnSelectFile_Click(object sender, EventArgs e)
    {
        OpenFileDialog file = new OpenFileDialog();
        file.ShowDialog();
        this.txtFile.Text = file.SafeFileName;
    }
}


2. 利用asp:FileUpload

    <asp:FileUpload runat="server" ID="uploadImage" />
    <asp:Button ID="btnOK" runat="server" OnClick="btnOK_Click" />

 

protected void btnOK_Click(object sender, EventArgs e)
    {

        if (uploadImage.HasFile)
        {
            string fileName = uploadImage.FileName;

            //*********************************************************************
            //*** 方法一 *****
            //-- 註解:先設定好檔案上傳的路徑,這是Web Server電腦上的硬碟「實體」目錄。
            //       C#語法在撰寫磁碟的目錄位置時,請留意以下的寫法:
            //String savePath = "c:\\temp\\uploads\\";
 
            //*** 方法二 *****
            String savePath = Server.MapPath("~/Book_Sample/Ch18_FileUpload/Uploads/");
            //--網站上的 URL路徑。 Server.MapPath() 轉換成Web Server電腦上的硬碟「實體」目錄。
 
            //*** 方法三 *****
            //--註解:網站上的目錄路徑。所以不寫磁碟名稱(不寫 “實體”路徑)。
            //--以下的 URL路徑,請依照實際狀況,進行修改。否則程式會報錯!
            //String saveDir = "\\Book_Sample\\Ch18_FileUpload\\Uploads\\";
            //String appPath  = Request.PhysicalApplicationPath;
            ////-- appPath是網站 "根"目錄「轉換成」Server端硬碟路徑。
 
            //String savePath   = appPath + saveDir;
            //*********************************************************************

            string savePath = "c:\\Chris\\test_upload\\";

            String saveResult = savePath + fileName;
            fuImage.SaveAs(saveResult);
        }
        uploadImage.Dispose();
    }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值