zoukankan      html  css  js  c++  java
  • 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();
        }
  • 相关阅读:
    MySQL 中随机抽样:order by rand limit 的替代方案
    mysql下distinct和group by区别对比
    MVC中实现多按钮提交(转)
    js的逻辑 OR 运算符- ||
    js 实现键盘记录 兼容FireFox和IE
    jquery扩展
    sp_executesql的用法
    MVC中,视图的Layout使用
    MVC4的过滤器
    MVC中的Repository模式
  • 原文地址:https://www.cnblogs.com/sipher/p/3413597.html
Copyright © 2011-2022 走看看