含有 ‘FileStream’標籤(tag)的文章們

DownLoad File From Asp.Net

….
if (file.Exists)
{
//Method-1 將檔案資料讀入Stream,這時檔案已經可以刪除了..之後再把檔案內容輸出即可
//Read File Content
sr = new StreamReader(path);
Response.Clear();
Response.AddHeader(“Content-Disposition”, “attachment; filename=ExcelExport.xls“); //下載的檔案名稱
Response.AddHeader(“Content-Length”, file.Length.ToString());  //下載的檔案大小
Response.ContentType = “application/vnd.ms-excel“; //下載的檔案類型
Response.Write(sr.ReadToEnd());
Response.End();
//Method-2 這是第二個方法,直接把檔案內容寫到前端,此時是不允許將檔案給刪除的,因為檔案是讀取的目標
//Response.Clear();
//Response.AddHeader(“Content-Disposition”, “attachment; filename=ExcelExport.xls”);
//Response.AddHeader(“Content-Length”, file.Length.ToString());
//Response.ContentType = “application/vnd.ms-excel”;
//Response.WriteFile(file.FullName);
//Response.End();
}
….

Continue reading »

利用FileSteam 讀檔 , 回傳字串資料

//File Content Transfer to String
private String LoadDecodedFile(String file)
{
String DecodedString = “”;
FileStream fs = new FileStream(file, FileMode.Open);
StreamReader r = new StreamReader(fs);
// Position the file pointer at the beginning of the file.
r.BaseStream.Seek(0, SeekOrigin.Begin);
// Read the entire file into a string and decode each chunk.
while (r.Peek() > -1)
DecodedString += Server.HtmlDecode(r.ReadLine());
r.Close();
return DecodedString;
}

Continue reading »