gpt4 book ai didi

c# - 从服务器下载文件,然后在服务器上删除

转载 作者:太空宇宙 更新时间:2023-11-03 18:48:28 28 4
gpt4 key购买 nike

好的,我正在从服务器下载文件,我计划在客户端下载文件后删除我在服务器上下载的文件..

我的下载代码工作正常,但我不知道什么时候输入删除文件的命令。

string filepath = restoredFilename.ToString();

// Create New instance of FileInfo class to get the properties of the file being downloaded
FileInfo myfile = new FileInfo(filepath);

// Checking if file exists
if (myfile.Exists)
{

// Clear the content of the response
Response.ClearContent();

// Add the file name and attachment, which will force the open/cancel/save dialog box to show, to the header
Response.AddHeader("Content-Disposition", "attachment; filename=" + myfile.Name);

//Response.AddHeader("Content-Disposition", "inline; filename=" + myfile.Name);
// Add the file size into the response header
Response.AddHeader("Content-Length", myfile.Length.ToString());

// Set the ContentType
Response.ContentType = ReturnExtension(myfile.Extension.ToLower());

//// Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)
Response.TransmitFile(myfile.FullName);

// End the response
Response.End();

}

现在我知道 response.End() 会停止一切并返回值,那么还有另一种方法吗..

我需要调用一个函数

DeleteRestoredFileForGUI(restoredFilename);

删除文件但不知道放在哪里..我尝试在 Response.End() 之前和之后放置但它不起作用..

感谢任何帮助...谢谢

最佳答案

添加

Response.Flush();
DeleteRestoredFileForGUI(restoredFilename);

在调用 TransmitFile() 之后并放弃对 Response.End() 的调用(您不需要它)。

如果这不起作用,则放弃 TransmitFile() 并使用:

Stream s = myFile.OpenRead();
int bytesRead = 0;
byte[] buffer = new byte[32 * 1024] //32k buffer
while((bytesRead = s.Read(buffer, 0, buffer.Length)) > 0 &&
Response.IsClientConnected)
{
Response.OutputStream.Write(buffer, 0, bytesRead);
Response.Flush();
}

关于c# - 从服务器下载文件,然后在服务器上删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2180979/

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