[英]Returning a File read into MemoryStream for download in ajax C#
I created an Excel file in a C# application using OpenXML. 我使用OpenXML在C#应用程序中创建了一个Excel文件。 I subsequently read it into a MemoryStream type.
随后,我将其读取为MemoryStream类型。 Now I want to return this file for download from the C# functions that returns an ActionResult Type.
现在,我想从C#函数返回此文件以下载,该函数返回一个ActionResult类型。 The data is returned to an ajax function where I am expecting a download prompt for the user so that they can save this file if they want.
数据返回到ajax函数中,在该函数中,我希望为用户提供下载提示,以便他们可以根据需要保存此文件。 Below is some sample code and things I tried.
以下是一些示例代码和我尝试过的事情。
My aim with MemoryStream was that I don't need to store the above created physical file on the server. 我使用MemoryStream的目的是不需要在服务器上存储上面创建的物理文件。 Is there a way to have the contents of the MemoryStream be returned to Ajax function for download?
有没有一种方法可以将MemoryStream的内容返回给Ajax函数进行下载?
I do not want to use TRIAL 3 where I sent the path of the file to ajax function where it uses it to redirect. 我不想在将文件的路径发送给ajax函数的TRIAL 3中使用它进行重定向。 This however works and I get the download message from the IE web browser.
但是,这可行,我从IE Web浏览器获得下载消息。 But nothing happens for Trial 1 or Trial 2 except perhaps Error.
但是,除了“错误”之外,试验1或试验2均未发生任何事情。
Is it because I need to have a physical file on the server for it to be downloaded. 是因为我需要在服务器上有一个物理文件才能下载它。 Not sure if ajax code needs anything more for Trial 1 or Trial 2 to work.
不知道ajax代码是否还需要其他东西才能使Trial 1或Trial 2正常工作。
Any tips on how I should be doing it the right way would be very helpful. 任何有关如何正确执行操作的提示都将非常有帮助。 Thanks
谢谢
public ActionResult createFile() 公共ActionResult createFile()
{ {
... ...
// Reads a file into memory stream. Common for all trials below
memoryStream = ReadDataIntoMemoryStream();
// TRIAL 1
FileStream fs= new FileStream();
memoryStream.CopyTo(fs);
string mimeType = "application/vnd.openxmlformats-
officedocument.spreadsheetml.sheet";
return File(fs, mimeType);
// TRIAL 2
string mimeType = "application/vnd.openxmlformats-
officedocument.spreadsheetml.sheet";
byte[] fileBytes = memoryStream.ToArray();
return File(fileBytes, mimeType);
// TRIAL 3
string filepath = "..\...\..\filename.xlsx"; // actual physical file on
// the server
return Content(filepath);
} }
Ajax 阿贾克斯
$(document).ready(function () {
$('#777').on('click', function () {
$.ajax({
method: "GET",
url: "Report/createFile",
contentType: "application/download", // not needed for Trial 3
// where string is returned
dataType:"text",
success: function (data) {
// window.location.href = data; // THIS WORKS with TRIAL 3
// where filepath is returned
},
error: function (data) {
alert("Error.");
},
});
});
});
Good job showing your work. 做好您的工作展示。
When you're done putting content into memoryStream
, its Position
property will refer to the end of the stream. 将内容放入
memoryStream
,其Position
属性将指向流的末尾。 Reset it back to the beginning and you can read from it. 将其重新设置为开始即可阅读。
memoryStream = ReadDataIntoMemoryStream();
memoryStream.Position = 0L;
Then you can use the File
method the way you wanted. 然后,您可以按需要使用
File
方法。
return File(memoryStream, mimeType, suggestedFileName);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.