gpt4 book ai didi

jquery - IHTTPHANDLER/下载文件。网页没有任何反应

转载 作者:行者123 更新时间:2023-12-01 04:20:27 26 4
gpt4 key购买 nike

我编写了一个 Ihttphandler 来下载文件:

public class DescargaFichero : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
Okiservice usuariosbd = new Okiservice();

usuariosbd.CommandTimeout = 9000;



byte[] datos;
//var lista = usuariosbd.ficheros
// .Where (p=> p.iddoc==iddoc)
// .Select(p => new { IdDoc = p.iddoc, Area = p.area, Nombre = p.nombreweb, Descripcion = p.descripcion, FicheroReal = p.SystemFile, Tamano = p.Size,Extension=p.Extension,MIME=p.Content_Type }).FirstOrDefault();

int id= Convert.ToInt32(context.Request["Id"]);

var lista = (from binario in usuariosbd.Documentos_Binario
join docs in usuariosbd.Documentos on binario.FK_Adjunto equals docs.FK_Adjunto
join tipo in usuariosbd.Content_Type on binario.FileType equals tipo.id_content
where docs.iddoc == id
select new { binario.SystemFile, tipo.Content_Type1, docs.nombreweb }).First();

datos = lista.SystemFile;

//System.Web.HttpContext.Current.Response.ContentType =
// "application/vnd.ms-excel";


string tipocontenido= lista.Content_Type1;

string nombrefichero = lista.nombreweb;


System.IO.Stream iStream = null;

// Buffer to read 10K bytes in chunk:
byte[] buffer = new Byte[10000];

// Length of the file:
int length;

// Total bytes to read:
long dataToRead;

// Identify the file to download including its path.
string filepath = "DownloadFileName";

// Identify the file name.
//string filename = System.IO.Path.GetFileName(filepath);

try
{
// Open the file.
//iStream = new System.IO.FileStream(sfichero, System.IO.FileMode.Open,
// System.IO.FileAccess.Read, System.IO.FileShare.Read);

MemoryStream input = new MemoryStream(datos);
iStream = input;


// Total bytes to read:
dataToRead = iStream.Length;

System.Web.HttpContext.Current.Response.ContentType = tipocontenido;
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + nombrefichero);

// Read the bytes.
while (dataToRead > 0)
{
// Verify that the client is connected.
if (System.Web.HttpContext.Current.Response.IsClientConnected)
{
// Read the data in buffer.
length = iStream.Read(buffer, 0, 10000);

// Write the data to the current output stream.
System.Web.HttpContext.Current.Response.OutputStream.Write(buffer, 0, length);

// Flush the data to the HTML output.
System.Web.HttpContext.Current.Response.Flush();

buffer = new Byte[10000];
dataToRead = dataToRead - length;
}
else
{
//prevent infinite loop if user disconnects
dataToRead = -1;
}
}
}
catch (Exception ex)
{
// Trap the error, if any.
System.Web.HttpContext.Current.Response.Write("Error : " + ex.Message);
}
finally
{
if (iStream != null)
{
//Close the file.
iStream.Close();
}
System.Web.HttpContext.Current.Response.Close();
}



}

public bool IsReusable
{
get
{
return false;
}
}
}

它由 JQuery AJAX 函数调用,我在其中传递 Id 参数,它是必须下载的文件的 id:

function obtenerFichero(id) {
$(document).ready(function () {

$.ajax({
type: "POST",
url: "../HELPDESK/DescargaFichero.ashx",

data:"Id="+id,

success: function (msg) {

}

});
});
}

当我运行调试时,我可以看到我在 IHttpHandler 中获取了正确的数据在响应时间:=> System.Web.HttpContext.Current.Response.OutputStream.Write(缓冲区, 0, 长度);但网页上什么也没发生,什么也没有。

谢谢!

最佳答案

我认为问题在于您的代码尝试从 AJAX 调用下载文件,但这是不可能的。

要触发文件下载,请将 window.location 更改为处理程序的 URL。由于您使用 Content-Disposition,因此应该提示下载。

function obtenerFichero(id) {
window.location.href = '../HELPDESK/DescargaFichero.ashx?ID=' + id;
}

关于jquery - IHTTPHANDLER/下载文件。网页没有任何反应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11117509/

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