临时链接下载文件 [英] temporary link download file

查看:94
本文介绍了临时链接下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi





我有一个临时下载文件的链接。

请完成一个小样本集。

谢谢

hi


I've got a link to download the file temporarily.
Please complete a small sample set.
Thank you

推荐答案

如果您只想在服务器端使用操作系统管理的临时文件,请使用以下文件:< a href =http://msdn.microsoft.com/en-us/library/system.io.path.gettempfilename%28v=vs.110%29.aspx> Path.GetTempFileName() [< a href =http://msdn.microsoft.com/en-us/library/system.io.path.gettempfilename%28v=vs.110%29.aspxtarget =_ blanktitle =New Window> ^ ]。



请注意,您可以拥有的文件数量有限。虽然在TEMP文件夹中有GC,但由于应用程序生命周期的原因,迁移不能依赖它。因此,您最好创建一个IDisposable包装器,在您完成它时删除该文件。请参阅此处的解决方案: http://stackoverflow.com/questions/400140 / how-do-i-automatic-delete-tempfiles-in-c [ ^ ]



If you just want an OS managed temporary file on server side, use this one: Path.GetTempFileName()[^].

Be aware, that the number of files you can have is limited. Altrough there is GC in TEMP folder, you mignt not be able to rely on it because of the application lifecycle. So you better create an IDisposable wrapper, that removes the file when you'r done with it. See the solution here: http://stackoverflow.com/questions/400140/how-do-i-automatically-delete-tempfiles-in-c[^]

using System;
using System.IO;
sealed class TempFile : IDisposable
{
    string path;
    public TempFile() : this(System.IO.Path.GetTempFileName()) { }

    public TempFile(string path)
    {
        if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path");
        this.path = path;
    }
    public string Path
    {
        get
        {
            if (path == null) throw new ObjectDisposedException(GetType().Name);
            return path;
        }
    }
    ~TempFile() { Dispose(false); }
    public void Dispose() { Dispose(true); }
    private void Dispose(bool disposing)
    {
        if (disposing)
        {
            GC.SuppressFinalize(this);
        }
        if (path != null)
        {
            try { File.Delete(path); }
            catch { } // best effort
            path = null;
        }
    }
}
static class Program
{
    static void Main()
    {
        string path;
        using (var tmp = new TempFile())
        {
            path = tmp.Path;
            Console.WriteLine(File.Exists(path));
        }
        Console.WriteLine(File.Exists(path));
    }
}





这里有一些示例如何执行具体下载:http://www.csharp-examples.net/download-files/ [ ^ ]。实际上这很简单。



And here you have examples how to perform the concrete download: http://www.csharp-examples.net/download-files/[^]. Actually it is quite simple.


这篇关于临时链接下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆