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();
}
C# 選擇本機檔案並上傳的更多相关文章
- UTF-8 的中文檔案名上傳問題
在上传文件后,一般都会用 move_uploaded_file() 进行文件移动改名.但是 move_uploaded_file() 并不支持 UTF-8 编码,如果含有,那么函数就会执行失败,但是这 ...
- [ASP.NET] 如何利用Javascript分割檔案上傳至後端合併
最近研究了一下如何利用javascript進行檔案分割上傳並且透過後端.特地記錄一下相關的用法 先寫限制跟本篇的一些陷阱 1.就是瀏覽器的支援了 因為本篇有用到blob跟webworker 在ie中需 ...
- 如何在 Visual Studio 2012 控制 TFS 版控時要忽略哪些檔案
幾乎在任何一種版本控管的機制裡,都會遇到那些「不應該簽入到版本庫」的潛規則,以往我們在用 SVN 的時候,我就寫過幾篇文章要大家注意這點.最近都改用 TFS 做版控,因為大多使用 Visual Stu ...
- 在 Server 端存取 Excel 檔案的利器:NPOI Library
转处 http://msdn.microsoft.com/zh-tw/ee818993.aspx Codeplex 軟體套件(Package)資訊 套件名稱 NPOI 作者 tonyqus, huse ...
- [Xamarin.Android]如何引用JAR檔案 (转帖)
這個範例是如何在Xamarin.Android中去使用一個我們自行在開發的JAR檔案. 主要會執行的步驟如下 1. 在Xamarin建立一個Android Java Bindings Library ...
- 利用XSD配合XSLT產出特定格式Word檔案 -摘自网络
利用類別產生XSD檔 產出XSD檔的目的在於提供Word樣板設計之資料框架 在此使用微軟提供之XML Schema Definition Tool (Xsd.exe)工具產生XSD檔 1. 定義類別 ...
- C++ 檔案、資料夾、路徑處理函式庫:boost::filesystem
原帖:https://tokyo.zxproxy.com/browse.php?u=uG7kXsFlW1ZmaxKEvCzu8HrCJ0bXIAddA1s5dtIUZ%2FYzM1u9JI7jjKLT ...
- ColorPic 一套簡單好用的顏色選擇器!
做美工的時候,常常會有配色.抓取顏色及獲取顏色代碼的困擾,專業人士可能有很好的工具來協助,但對於偶爾需要或非經常接觸美工的人來說,即便是有很好的工具,也還要花很多時間進行學習,常常就是看到一個漂亮的顏 ...
- 何解決 LinqToExcel 發生「無法載入檔案或組件」問題何解決 LinqToExcel 發生「無法載入檔案或組件」問題
在自己的主機上透過 Visual Studio 2013 與 IISExpress 開發與測試都還正常,但只要部署到測試機或正式機,就是沒辦法順利執行,卡關許久之後找我協助.我發現錯誤訊息確實很「一般 ...
随机推荐
- CentOS6.5 解压安装 二进制分发版 mysql-5.5.49-linux2.6-x86_64.tar.gz
环境:CentOS 6.5 64位 1.下载安装包 http://dev.mysql.com/downloads/mysql/5.5.html#downloads http://dev.mysql.c ...
- pxe kickstart 配置+TFTP+NFS多版本系统部署
系统centos6.5x86_64 1.DHCP服务配置. yum安装dncp [root@cnhzdhcp16593 ~]# yum -y install dhcp 配置dhcp服务,新建dhcpd ...
- Dynamic CRM 2013学习笔记(三十四)自定义审批流5 - 自动邮件通知
审批过程中,经常要求自动发邮件:审批中要通知下一个审批人进行审批:审批完通知申请人已审批完:被拒绝后,要通知已批准的人和申请人.下面详细介绍如何实现一个自动发邮件的插件: 1. 根据审批状态来确定 ...
- [BTS] Correct the specified Action, or refer to the documentation on the allowed formats for the Actions
A message sent to adapter "WCF-SAP" on send port "CNILG.iHouse.SAP.WCFSAP" with ...
- 在DOS下的DEBUG命令的详细用法
在DOS下的DEBUG命令的详细用法 名称 解释 格式 a (Assemble) 逐行汇编 a [address] c (Compare) 比较两内存块 c range address d (Dump ...
- Atitit.并发编程原理与概论 attilax总结
Atitit.并发编程原理与概论 attilax总结 1. 并发一般涉及如下几个方面:2 2. 线程安全性 ( 2.2 原子性 2.3 加锁机制2 2.1. 线程封闭3.3.1Ad-hoc线程封闭 3 ...
- HTML+CSS提升小实战
1 <html> 2 <head> 3 <meta http-equiv="Content-Type" content="text/html ...
- Spring简介和基础
Spring介绍 1.什么事Spring? spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架. spring的设计模式是单例模式和工厂模式. 2.spring的四大优点 轻量 ...
- static/final
- 在dos中运行java程序,若出现Exception in thread “main" java.lang.NoClassDefFoundError
在dos中运行java程序,若出现Exception in thread “main" java.lang.NoClassDefFoundError,可以检查一下几项: 环境变量配置: 注意 ...