gpt4 book ai didi

jquery - ASP.NET WebAPI创建文件并保存以供将来下载并在下载后删除

转载 作者:行者123 更新时间:2023-12-01 05:48:15 28 4
gpt4 key购买 nike

我有一个 ASP.NET WebAPI 项目,需要根据客户端 (jQuery) 请求生成 XLS 或 PDF。

因为无法使用 $.ajax 下载文件,所以方法非常简单:

  1. 我将通过 $.ajax 请求在服务器上生成文件(XLS 或 PDF)。

    but instead of sending the response from server to the client I will save the file locally. The filename will be: some_token.xls. The token should be unique. Maybe a GUID.

  2. 生成文件并将其保存在服务器上后,该方法将使用 200 状态 和刚刚生成的 token (实际上是文件名)进行响应。
  3. 我的jQuery ajax请求方法将进入成功函数,然后将使用刚刚生成的文件的路径和 token 附加到IFrame的“src”属性。
  4. 服务器将下载该文件并将其删除。
所以我的 jQuery ajax 看起来像这样:

 $.ajax({
// the url used to generate the file and save it on server
url: '/api/requestfile',
contentType: "application/json; charset=utf-8",
data: myData,
dataType: "json",
cache: false,
type: 'POST',
success: function (data) {
if (data) {
// if the file was created successfuly on server then download the file
var iframe = $("<iframe/>").attr({
src: 'api/downloadfile/' + data.filetoken, // filetoken generated in server
style: "visibility:hidden;display:none"
}).appendTo(buttonToDownloadFile);
} else {
alert('Something went wrong');
}
}
})

所以我有几个问题:

a) Where is the right server path to save the file that I will generate? (AppData, customFolder..?

<小时/>

b) Is there any best practice to create tokens as file names or just using a GUID is fine?

<小时/>

c) How can I delete the file in server once it's downloaded? The file won't be accessed by any other user because the file is created by request. So it's a one time file.

更新的解决方案:

a) The solution was to create a "TempFiles" directory inside App_Data folder that is used to store data.

b) I create a random function to generate a token:

public static string GetRandomToken(int length) {
int intZero = '0';
int intNine = '9';
int intA = 'A';
int intZ = 'Z';
int intCount = 0;
int intRandomNumber = 0;
string strDownloadToken = "";
Random objRandom = new Random(System.DateTime.Now.Millisecond);
while (intCount < length) {
intRandomNumber = objRandom.Next(intZero, intZ);
if (((intRandomNumber >= intZero) &&
(intRandomNumber <= intNine) ||
(intRandomNumber >= intA) && (intRandomNumber <= intZ))) {
strDownloadToken = strDownloadToken + (char) intRandomNumber;
intCount++;
}
}
return strDownloadToken;
}

c) Because I'm using WebAPI the delete is a little bit different. I had to create a Filter:

public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) {
// here I added the code to delete the file.
// this Filter is executed after the file response have been sent to server so its safe now to delete the file
}

最佳答案

在评论中,@MikeCheel 关于如何构建它的说法可能是正确的,但如果您打算构建服务器端解决方案

A) Where is the right server path to save the file that I will generate? (AppData, customFolder..)

外界无法访问的自定义文件夹。然后创建一个处理程序,根据 url 参数提供文件。 IE。 mywebsite.com/getFile?id=23434234 或类似的。在处理程序中,您可以轻松添加安全性和其他检查。对于 C 语言来说它也会派上用场。

您当然可以使用可访问的自定义文件夹,但这通常会令人不悦

b) Is there any best practice to create tokens as file names or just using a GUID is fine?

没有真正的最佳实践,您希望它们不容易被猜到。指南就可以很好地工作。

c) How can I delete the file in server once it's downloaded? The file won't be accessed by any other user because the file is created by request. So it's a one time file.

如果您使用上面的处理程序,您可以提供该文件,然后将其删除。否则,您可能需要一个定期删除临时文件的过程。如果没有处理程序,我不确定如何跟踪文件是否实际下载了一次,或者是否从未下载过,或下载了 100 次。

关于jquery - ASP.NET WebAPI创建文件并保存以供将来下载并在下载后删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24714072/

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