- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用 ajax 和 ActionResult 在浏览器上下载文件。该文件已下载并从我的 ActionResult 返回。
我看到 Http 查询没问题,并看到响应正文中的数据。问题是该文件不建议保存在浏览器中。
一切似乎都很好。我在教程和论坛中看到的所有内容都像我所做的那样,但我的不要说 XD。我不明白我和其他人有什么区别。
这是我的 ActionResult :
public ActionResult ShippingDownloadDNPriority(string SALE_GUID)
{
int supId = -1;
int.TryParse(Session["SupId"].ToString(), out supId);
if (supId < 0)
return null;
WebResponse response = CallApi.DownloadAndCreateDN(Session["UserLogin"].ToString(), Session["IdentConnect"].ToString(), SALE_GUID, supId, true);
Stream responseStream = response.GetResponseStream();
var cd = new System.Net.Mime.ContentDisposition
{
FileName = "myfile.pdf",
Inline = false,
};
Response.Headers.Add("Content-Disposition", cd.ToString());
Response.ContentType = "application/octet-stream";
return File(responseStream, System.Net.Mime.MediaTypeNames.Application.Pdf, "myfile.pdf");
}
public static WebResponse DownloadAndCreateDN(string login, string session, string SALE_GUID, int supid, bool priority)
{
string[] res = new string[2];
StringBuilder postData = new StringBuilder();
postData.AppendLine("{");
postData.AppendLine(string.Format("\"login\":\"{0}\",", login));
postData.AppendLine(string.Format("\"session\":\"{0}\",", session));
postData.AppendLine(string.Format("\"saleguid\":\"{0}\",", SALE_GUID));
postData.AppendLine(string.Format("\"supid\":{0},", supid));
postData.AppendLine(string.Format("\"prority\":{0}", priority.ToString().ToLower()));
postData.AppendLine("}");
ASCIIEncoding ascii = new ASCIIEncoding();
byte[] postBytes = ascii.GetBytes(postData.ToString());
string url = Properties.Settings.Default.ISAPIAddress + "deliverynote/create";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = postBytes.Length;
Stream postStream = request.GetRequestStream();
postStream.Write(postBytes, 0, postBytes.Length);
postStream.Flush();
postStream.Close();
return request.GetResponse();
}
$.ajax({
url: '../Shipping/ShippingDownloadDNPriority?SALE_GUID=XXXXXXXXXXXXXX',
data: { SALE_GUID: DropShipping.GetRowKey(rowIndexSale) },
async: false,
//success: function (data) { window.downloadFile = data; }
});
最佳答案
AJAX 只是一个瘦客户端。默认情况下返回的响应没有任何 react 。您有责任进行下载。但是,这样做需要作为 HTML5 一部分的文件 API。因此,这只能在现代浏览器 (IE10+) 中实现。
在您的 AJAX 成功方法中:
var blob = new Blob(data, { type: 'application/pdf' });
var a = document.createElement('a');
var url = window.URL.createObjectURL(blob);
a.href = url;
a.download = 'myfile.pdf';
a.click();
window.URL.revokeObjectURL(url);
$.ajax({
url: '../Shipping/ShippingDownloadDNPriority?SALE_GUID=XXXXXXXXXXXXXX',
data: { SALE_GUID: DropShipping.GetRowKey(rowIndexSale) },
async: false,
// -- ADD THIS --
xhrFields: {
responseType: 'blob'
},
success: function (data) {
// code above here, but no longer need to create blob
var a = document.createElement('a');
var url = window.URL.createObjectURL(data);
a.href = url;
a.download = 'myfile.pdf';
a.click();
window.URL.revokeObjectURL(url);
}
});
关于ajax - 从 ajax 和 ActionResult 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41901082/
我有一个 ActionResult 调用另一个 ActionResult。 我在我的 case 语句中调用了一个 ActionResult,但它不起作用。这是我所拥有的: public Acti
我有一个 ActionResult 扩展,它在返回页面时向 TempData 添加 toast: public static IActionResult WithMessage(this Action
假设我有以下代码,在记事本中模拟,所以请原谅任何小错误:) //Default page public ActionResult Index() { var musicView
我有一个来自数据库的对象,它在我的应用程序的很多地方使用。 实际的精确对象构建起来有点复杂,尤其是在开发过程中,我已经多次更改它。为此,我将方法从 Controller 中提取出来,并构建了一个具有对
我像这样向 GET ActionResult 发送参数: public ActionResult MyFormLetter(string studentName, string teacherName
在 Scott Hanselman 的书中(第 1 章),他为我们提供了两个选项来实现 Create 操作方法的 [HttpPost]。 第一个依赖于 TryUpdateModel 根据传入的表单字段
错误在下面代码的第 5 行: public class FirstController : Controller{ public async Task Index() {
我得到了简单的网络服务应用程序: public class MyController : Controller { public ActionResult balance( int amount)
所以我有这个 Javascript 代码: function AddToStuff(somethingID) { var stuffID = document.getElementById('
我试图在选中复选框时调用 ActionResult,但我不知道为什么它不起作用下面是我的代码。 --复选框 --模态
我正在使用 MVC 4 创建游戏。有一个犯罪功能,但我无法解决。我已经尝试了很多东西,但由于我是 MVC4 的新手,所以我无法弄明白。 我创建了一个单选按钮列表,每个不同的选项应该给出不同的结果。这是
我知道有很多类似的问题,但我没有在其中任何一个中得到我想要的答案。我想按原样将 ActionResult 转换为 String,方法如下: public ActionResult ProductDet
我想从一些操作方法中返回一个强制当前页面刷新的结果。 我写这篇文章是为了获得这样的结果: public class RefreshResult : ActionResult { pu
我使用 MVC 5 返回 Json。到达数据返回点的总时间为 40 毫秒。 然而,即使在服务器上运行它,浏览器也需要 6000 毫秒 才能获取数据。 我的问题是返回值后会发生什么。我正在尝试找出导致速
我创建了一个派生自 ActionResult 的 SitemapResult 类。它允许调用者添加任意数量的 URL 资源,然后以 XML 格式输出站点地图数据。 public class Sitem
例如,对于在线 Web 应用程序,我只需要服务器驱动器 X:\Docs 上文件夹中的文档。有没有一种方法可以让网站上的按钮默认打开 X:\Docs?我试过打开特定文件夹但没有成功: [HttpPost
我的 Controller 中有以下内容; public ActionResult CocktailLoungeBarAttendant() { return View
我是 MVC 的新手,正在尝试从 Controller 调用存储过程。 在模型中,我有 edmx,它将所有存储过程作为函数 Cannot implicitly convert type System.
这个有效: public ActionResult Edit(int id, CompPhone cmpPhn) { var vM = new MyViewModel(); if (cmpPh
假设我有应用程序 .net Core 2.1 Web API + Angular 7 为什么我应该总是返回 ActionResult? 这之间有什么区别吗: public ActionResult G
我是一名优秀的程序员,十分优秀!