gpt4 book ai didi

c# - 通过网络服务和 jquery 下载文件?

转载 作者:太空宇宙 更新时间:2023-11-03 13:35:15 25 4
gpt4 key购买 nike

我试图在 asp.net 2.0 中创建一个网络服务来下载文件(弹出窗口打开或保存文件),以这种方式:

$.ajax({
type: "POST",
url: "webservice.asmx/download",
data: "{}",
contentType: "application/json; charset=utf-8",
success: function (res) {
console.log("donwloaded");
}
});

在“webservice.asmx”中

[WebMethod()]
public byte[] DownloadFile(string FName)
{
System.IO.FileStream fs1 = null;
fs1 = System.IO.File.Open(FName, FileMode.Open, FileAccess.Read);
byte[] b1 = new byte[fs1.Length];
fs1.Read(b1, 0, (int)fs1.Length);
fs1.Close();
return b1;
}

[WebMethod]
public void download()
{
string filename = "test.txt";
string path = "C:\\test.txt";

byte[] ls1 = DownloadFile(path);
HttpResponse response = Context.Response;

response.Clear();
response.BufferOutput = true;
response.ContentType = "application/octet-stream";
response.ContentEncoding = Encoding.UTF8;
response.AppendHeader("content-disposition", "attachment; filename=" + filename);

response.BinaryWrite(ls1);

response.Flush();
response.Close();
}

通过这种方式我可以看到文件的内容(我无法通过弹出窗口下载文件)。

我哪里错了?可以这样做吗?

提前致谢

最佳答案

这不是一个好方法,要通过弹出窗口下载文件,只需打开一个新弹出窗口并使该窗口的 url 指向您的文件或 web 服务 url。

window.open("fileurl"  )

关于c# - 通过网络服务和 jquery 下载文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18920408/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com