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

发布于 2024-08-20 02:25:25 字数 1593 浏览 6 评论 0原文

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

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

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() 之前和之后,但它不起作用..

感谢任何帮助…谢谢

ok i am downloading a file from a server and i plan to delete the file that i have downloaded on the server after it gets downloaded on the client side..

My download code is working fine but i dont know when to put the command to delete the file.

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();

            }

Now i know the response.End() will stop every thing and return the value, so is there another way too do so..

I need to call a function

DeleteRestoredFileForGUI(restoredFilename);

to delete the file but dont know where to put it.. i tried putting before and after Response.End() but it does not work..

any help is appreciated... thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

大姐,你呐 2024-08-27 02:25:25

在调用 TransmitFile() 之后添加

Response.Flush();
DeleteRestoredFileForGUI(restoredFilename);

并放弃对 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();
}

Add

Response.Flush();
DeleteRestoredFileForGUI(restoredFilename);

after the call to TransmitFile() and ditch the call to Response.End() (you don't need it).

If that does not work, then ditch TransmitFile() and go with:

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();
}
删除会话 2024-08-27 02:25:25

您无法立即删除该文件,因为它可能尚未下载。从服务器端没有简单的方法可以判断文件是否已成功下载。如果浏览器打开打开/保存对话框怎么办?在确认对话框之前,下载不会开始。 (这可能不会立即发生和/或对话可能会被取消)
或者,如果它是一个大文件并且在完全下载之前断开连接怎么办?是否可以再次尝试下载?

通常建议的处理您的情况的方法是,在一段时间后将删除作为一个单独的过程进行,这样您就可以(相当)确定不再需要该文件和/或可以在需要时重新创建/恢复该文件。

根据您的情况,您可以有一个单独的进程来定期删除/处理旧文件。或者,如果流量较低,则可以在每次请求新文件时检查并删除旧文件。

旧文件的识别可能基于文件时间或数据库中的关联值。无论哪种方式,如果可能有大量文件需要处理,并且不太可能识别出大量要删除的文件,那么您不太可能需要频繁检查的开销。
另外,请务必解决大量文件未尽快删除的后果(磁盘空间真的是一个问题吗?),以防止可能在仍然需要时删除它们的副作用,或通过热心检查产生性能副作用。

you can't delete the file straight away as it may not have been downloaded yet. from the server side there is no easy way of telling that the file was successfully downloaded. what if an open/save dialog is opened by the browser? download won't begin until the dialog is acknowledged. (this may not be immediately and/or the dialog may be cancelled)
or, what if it is a large file and the connection is dropped before it is fully downloaded? should it be possible to attempt the download again?

the normally recommended way of dealing with your situation is to do the deletion as a separate process, after a time period which allows you to be (fairly) sure the file is no longer required and/or it can be recreated/restored if need be.

depending on your situation you could have a separate process which periodically removes/processes old files. or, if you have a low volume of traffic, you could check for and delete old files each time a new one is requested.

the identification of old files will likely be based on a file time or associated value in a darabase. either way, if there are potentially lots of files to process you are unlikely to want the overhead of checking very frequently if it is unlikely to identify a lot of files to remove.
also, be sure to way up the consequences of lots of files not being removed ASAP (is disk space really an issue?) against the side effects of possibly deleting them while still needed or creating a performance side effect by checking to zealously.

遥远的她 2024-08-27 02:25:25

你所遵循的一般模式让我想知道,你在这样做吗?

  1. 为客户端创建数据并保存到
  2. 磁盘 将文件传输到客户端 删除
  3. 文件

如果是的话,您可能会将系统更改为在内存中工作。由于内存是在 .Net 中管理的,因此您不必进行手动清理,并且根据文件的大小,这也可能会快一些:

  1. 为客户端创建数据并保存到 MemoryStream
  2. 将流传输到客户端

The general pattern you are following makes me wonder, are you doing this?

  1. Create Data for Client and Save to
  2. Disk Transmit File to Client Delete
  3. File

If you are, you might change your system to work in memory. Since memory is managed in .Net you wouldn't have to do this manual cleanup, and depending on the size of the file this could be a good bit faster too:

  1. Create Data for Client and Save to MemoryStream
  2. Transmit Stream to Client
乖乖兔^ω^ 2024-08-27 02:25:25

由于您在标头中设置了文件名,因此您有两个选择:

  1. 将文件内容读取为字符串、删除文件、回显/打印字符串作为消息正文。

  2. 将文件重命名为 delete-filename.xxx 之类的名称,然后使用一些外部进程(可能是 cron 作业?)来删除以该前缀开头的所有文件。

Since you set the file name in the header, you have two options:

  1. Read the file contents into a string, delete the file, echo/print the string as the body of the message.

  2. Rename the file something like delete-filename.xxx and then have some external process (maybe a cron job?) that goes behind and deletes any files beginning with that prefix.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文