作者kyo0010 (~KYO~)
看板C_Sharp
標題[問題] ashx下載疑問
時間Fri Mar 25 12:07:04 2016
先附上下載語法
private void DownLoadFile(string parFilePath, string fileName)
{
string pathStr = @"D:\web測試\html\下載\option\test.jpg";
string FilePath = pathStr;
try
{
FileStream fr = new FileStream(FilePath, FileMode.Open);
Byte[] buf = new Byte[fr.Length];
fr.Read(buf, 0, Convert.ToInt32(fr.Length));
fr.Close();
fr.Dispose();
context.Response.Clear();
context.Response.ClearHeaders();
context.Response.Buffer = true;
context.Response.AddHeader("content-disposition",
"attachment; filename=" + fileName);
context.Response.BinaryWrite(buf);
context.Response.End();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
此段語法在在一般的aspx可以成功下載
但用在html+ajax之後點了會沒反應
catch會出現
"{無法評估運算式,因為程式碼已經最佳化,或者原生框架不在呼叫堆疊的最上層。}
爬一天文 能加的header也加到有點亂了
所以貼上的是還原後 最原先aspx能執行的語法
現在還是無解 求神人大大指引一條明路
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 59.120.119.244
※ 文章網址: https://www.ptt.cc/bbs/C_Sharp/M.1458878827.A.419.html
→ Litfal: 你有跨網站嗎? 03/25 23:09
沒有跨 在同一個專案目錄下 用$.post有呼叫到ashx
※ 編輯: kyo0010 (39.12.2.53), 03/27/2016 20:41:07
推 Ammenze: 不要用ajax,直接設定連結的url到你的ashx就好 03/28 14:08