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();
        }
  • 相关阅读:
    使用 asp.net mvc和 jQuery UI 控件包
    ServiceStack.Redis 使用教程
    HTC T8878刷机手册
    Entity Framework CodeFirst 文章汇集
    2011年Mono发展历程
    日志管理实用程序LogExpert
    使用 NuGet 管理项目库
    WCF 4.0路由服务Routing Service
    精进不休 .NET 4.0 (1) asp.net 4.0 新特性之web.config的改进, ViewStateMode, ClientIDMode, EnablePersistedSelection, 控件的其它一些改进
    精进不休 .NET 4.0 (7) ADO.NET Entity Framework 4.0 新特性
  • 原文地址:https://www.cnblogs.com/sipher/p/3413597.html
Copyright © 2011-2022 走看看