FileUpload1.HasFile // 判斷是否有檔案
FileUpload1.FileName // 只取得檔名
FileUpload1.PostedFile.FileName // 取得完整的路徑和檔名
//用js抓 在firefox 有 full path 但chrome無效
alert(document.getElementById('ContentPlaceHolder1_FileUpload1').value);
抓檔案其實是為了寄出去,後來把檔案的Byte轉Stream寄出去
System.Net.Mail.Attachment attachment1 = new System.Net.Mail.Attachment(BytesToStream(FileUpload1.FileBytes), FileUpload1.FileName);
/// <summary>
/// 將 byte[] 轉成 Stream
/// </summary>
public Stream BytesToStream(byte[] bytes)
{
Stream stream = new MemoryStream(bytes);
return stream;
}
//c# 取得server上的完整路徑
Server.MapPath(FileUpload1.FileName);
//多選,html5,IE9以上才支援
可以加這個在html這邊,multiple="multiple"
或用
<asp:FileUpload ID="FileUpload1" AllowMultiple="true" runat="server" />
配合多選的上傳的後台CODE
private void fileuploadannex(string case_oid)
{
if (FileUpload1.HasFile)
{
String savePath = Server.MapPath("~/trace/annex/" + case_oid.Substring(0, 6).ToString() + "/" + case_oid.ToString() + "/");
if (!Directory.Exists(savePath))//檢查資料夾是否存在
{
Directory.CreateDirectory(savePath);//資料夾不在建立資料夾
}
HttpFileCollection multipleFiles = Request.Files;
HttpPostedFile uploadedFile;
string fileName;
string NewfileName;
string NewfileNamePath;
for (int fileCount = 0; fileCount < multipleFiles.Count; fileCount++)
{
uploadedFile = multipleFiles[fileCount];
fileName = Path.GetFileName(uploadedFile.FileName);
if (uploadedFile.ContentLength > 0)
{
if (!System.IO.File.Exists(savePath + fileName))//檢查上傳的資料是否有重覆
{
uploadedFile.SaveAs(savePath + fileName);
}
else
{ //重覆的資料先改名稱
NewfileName = fileName.Substring(0, fileName.LastIndexOf("."));
NewfileNamePath = fileName.Substring(fileName.LastIndexOf("."), fileName.Length - fileName.LastIndexOf("."));
System.IO.File.Move(savePath + fileName, savePath + NewfileName + "_" + DateTime.Now.ToString("yyyyMMddhhmmssfff") + NewfileNamePath);
uploadedFile.SaveAs(savePath + fileName);
}
}
}
}
沒有留言:
張貼留言