[英]How to download files on client side from physical path in ASP.NET
i have a generic controller which I call using AJAX. It recieves the path from a file我有一个通用的 controller,我使用 AJAX 调用它。它从文件接收路径
(for example: C:\\Control_Ventas\\2022\\2022_03\\13032022\\58_13032022\\hola.txt)
In this case is a txt but it could be a pdf or a excel document.在这种情况下是一个 txt,但它可以是 pdf 或 excel 文档。 I tried to make it work with this code, but it didnt worked.我试图让它与这段代码一起工作,但没有成功。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Dbventas
{
/// <summary>
/// Descripción breve de FileDownloadHandler
/// </summary>
public class FileDownloadHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
HttpResponse response = HttpContext.Current.Response;
string filePath = context.Request.Form.Get("path");
response.Clear();
response.ClearContent();
response.ClearHeaders();
response.Buffer = true;
byte[] data = System.IO.File.ReadAllBytes(@filePath);
response.BinaryWrite(data);
response.End();
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
Edit: this is de AJAX call and the code that I modified.编辑:这是 de AJAX 调用和我修改的代码。 Now the file is sent to the client, but i dont know how to build the file again.现在文件已发送到客户端,但我不知道如何重新构建文件。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Dbventas
{
/// <summary>
/// Descripción breve de FileDownloadHandler
/// </summary>
public class FileDownloadHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
HttpResponse response = HttpContext.Current.Response;
string filePath = context.Request.Form.Get("path");
filePath = filePath.Replace("/", @"\");
byte[] data = System.IO.File.ReadAllBytes(@filePath);
response.Clear();
response.AddHeader("Content-Length", data.Length.ToString());
response.AddHeader("Content-Disposition", "attachment;filename=FILENAME");
response.OutputStream.Write(data, 0, data.Length);
response.Flush();
response.End();
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
AJAX call AJAX 拨打
function downloadFile(id) {
var fd = new FormData();
fd.append("nombre", document.getElementById(id).dataset.filename);
fd.append("path", document.getElementById(id).dataset.path);
console.log(fd);
try {
$.ajax({
url: 'FileDownloadHandler.ashx',
type: "POST",
contentType: false, // Not to set any content header
processData: false, // Not to process data
data: fd,
success: function (result) {
console.log(result);
},
error: function (err) {
console.log("Error");
alert(err.statusText);
}
});
} catch (exception) {
console.log("ERROR"); alert(exception);
}
}
Thanks for reading!谢谢阅读!
I've had a case like this, but in vb.net我有过这样的案例,但在 vb.net
I'm change:我变了:
response.AddHeader("Content-Length", data.Length.ToString());
response.AddHeader("Content-Disposition", "attachment;filename=FILENAME");
response.OutputStream.Write(data, 0, data.Length);
response.Flush();
response.End();
To:到:
Dim stream As MemoryStream = New MemoryStream(Pck.GetAsByteArray())
Response.AppendHeader("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
Response.AppendHeader("Content-Disposition", "attachment; filename=Filename.xlsx")
Response.BinaryWrite(stream.ToArray())
Response.End()
maybe you can search code similar like above in c#也许你可以在 c# 中搜索类似上面的代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.