【问题标题】:Tusdotnet Upload and Download Files .Net CoreTusdotnet上传和下载文件.Net Core
【发布时间】:2020-12-10 14:00:23
【问题描述】:

Tusdotnet 是为使用 .NET 核心实现的文件上传而构建的,它的大小更大(如果卡住,它将从同一点恢复) .

  • 上传文件的 ASP .NET Core 实现:
    app.UseTus(httpContext => new DefaultTusConfiguration
    {
        // c:\tusfiles is where to store files
        Store = new TusDiskStore(@"C:\tusfiles\"),
        // On what url should we listen for uploads?
        UrlPath = "/files",
        Events = new Events
        {
            //OnFileCompleteAsync = eventContext =>
            OnFileCompleteAsync = async eventContext =>
            {
    
                //return Task.CompletedTask;
                ITusFile file = await eventContext.GetFileAsync();          
                if (file != null)
                {
                    //Convert in to a FileStream
                    //var fileStream = await file.GetContentAsync(httpContext.RequestAborted);
                }
            }
        }
    }

https://github.com/tusdotnet/tusdotnet

  • 在文件夹系统中上传后:

我需要问一下:

  • 为了下载文件,我们需要编写自定义代码,但如何识别该级别的文件扩展名
  • 以及我们需要使用什么技术来下载文件,正如 Tusdotnet 所说,我们不负责下载文件。

【问题讨论】:

  • And what technique we need to use for download File, as Tusdotnet said we are not responsible for downloading the file您将文件存储在哪里?
  • For download the file we need to write a custom code but how it will be possible to identify the file extension at that level. stackoverflow.com/questions/1886866/…
  • 上传文件我们使用上面的代码:参考:github.com/tusdotnet/tusdotnet
  • 您在哪里存储文件?是 C:\tusfiles\?如果是这样,app.UseStaticFiles(new StaticFileOptions() { FileProvider = new PhysicalFileProvider("c:\tusfiles\"), RequestPath = new PathString("/tusfiles") }); 是否有效? stackoverflow.com/a/31948829/34092
  • 您是否尝试在文件上传完成后重命名?

标签: c# asp.net-core .net-core


【解决方案1】:

通过 Tusdotnet 调查后,我发现只有两个选项:

第一种方法

文件成功上传到文件夹系统后重命名文件。 (因为 tusdotnet 上传成功时不维护文件扩展名。

第二种方法

在 tusdotnet 成功上传文件的同时,也可以使用以下代码上传带有扩展名的相同文件:

OnFileCompleteAsync = async eventContext =>
{    
    ITusFile file = await eventContext.GetFileAsync();
    if (file != null)
    {
        var fileStream = await file.GetContentAsync(httpContext.RequestAborted);
        var metadata = await file.GetMetadataAsync(httpContext.RequestAborted);
    
        httpContext.Response.ContentType = metadata.ContainsKey("contentType")
                                         ? metadata["contentType"].GetString(Encoding.UTF8)
                                         : "application/octet-stream";
    
        //Providing New File name with extension
        string name = "NewFileName.jpg";
        string networkPath = @"C:\tusfiles\";
    
        using (var fileStream2 = new FileStream(networkPath + "\\" + name, FileMode.Create, FileAccess.Write))
        {
            await fileStream.CopyToAsync(fileStream2);
        }
    }
}

参考:

【讨论】:

    猜你喜欢
    • 2020-08-25
    • 2022-02-01
    • 2014-07-21
    • 1970-01-01
    • 2019-02-10
    • 2017-07-27
    • 2023-03-04
    • 2019-06-26
    • 2018-10-24
    相关资源
    最近更新 更多