ajax運行在js的沙箱中,它不能訪問物理磁盤。因此用ajax下載不能成功。
採用模擬窗體提交的方式下載。
var url = "/ajax/tts/TraceLogFile.aspx?Method=downloadfile&ErrrFilePath=" + $(this).attr('src');
var form = $("<form></form>").attr("action", url).attr("method", "post");
//form.append($("<input></input>").attr("type", "hidden").attr("name", "fileName").attr("value", fileName));
form.appendTo('body').submit().remove();
服務器代碼
private void downloadfile()
{
string ErrrFilePath = Get("ErrrFilePath");
System.IO.FileInfo file = new System.IO.FileInfo(Acexe.Config.FS_DIR(base.gloCompanyInfo.SerId) + ErrrFilePath);
//HttpContext.Current.Response.ContentType = "application/ms-download";
HttpContext.Current.Response.ContentType = "text/plain";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Type", "application/octet-stream");
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8));
HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
HttpContext.Current.Response.WriteFile(file.FullName);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.End();
System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest();
}