-1

Tusdotnet is built for File uploads with .NET core Implementations, which are larger in size (If in case its gets stuck it will be resume from the same point).

  • ASP .NET Core Implementation for uploading File:
    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

  • After upload in folder system: enter image description here

I need to ask that:

  • 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.
  • And what technique we need to use for download File, as Tusdotnet said we are not responsible for downloading the file.
Taha Zubair
  • 32
  • 3
  • 7
  • `And what technique we need to use for download File, as Tusdotnet said we are not responsible for downloading the file` Where are you storing the files? – mjwills Dec 08 '20 at 06:05
  • `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.` https://stackoverflow.com/questions/1886866/how-to-find-the-extension-of-a-file-in-c – mjwills Dec 08 '20 at 06:06
  • For uploading files we are using the above code: Reference: https://github.com/tusdotnet/tusdotnet – Taha Zubair Dec 08 '20 at 06:09
  • Where are you **storing** the files? Is it C:\tusfiles\? If so, does `app.UseStaticFiles(new StaticFileOptions() { FileProvider = new PhysicalFileProvider("c:\tusfiles\"), RequestPath = new PathString("/tusfiles") });` work? https://stackoverflow.com/a/31948829/34092 – mjwills Dec 08 '20 at 06:11
  • In terms of filename, check out https://tus.io/faq.html (`How can I get the file name or file type for an upload?`) – mjwills Dec 08 '20 at 06:11
  • Downloads are not handled by tusdotnet. the above link is not useful for me. Reference:https://github.com/tusdotnet/tusdotnet/issues/82 – Taha Zubair Dec 08 '20 at 07:07
  • Did you try my suggestion? https://stackoverflow.com/questions/31946949/is-it-possible-to-serve-static-files-from-outside-the-wwwroot-folder/31948829#31948829 – mjwills Dec 08 '20 at 07:09
  • @mjwills, yes I tried your suggestion, Its working only for case **Point 2**, for **Point 1** its coming without extension so its not working.. 1) URL/files/c06d3d1d9ccd414f8306367cb50b69a7 2) URL/files/test2.jpg – Taha Zubair Dec 08 '20 at 07:25
  • Did you read my other two links? One for how to pass up the filename in metadata, and one how to get the file extension from a filename? – mjwills Dec 08 '20 at 08:26
  • Yes, passing File Name in meta data, I am passing it upon uploading of document. – Taha Zubair Dec 08 '20 at 09:16
  • Awesome - so use the metadata to rename it once it is uploaded. – mjwills Dec 08 '20 at 09:31
  • You can refer that link, I am using the same for upload https://github.com/tusdotnet/tusdotnet/blob/master/Source/TestSites/AspNetCore_netcoreapp3.1_TestApp/wwwroot/index.html#L90 The only issue is that if files are saving without format in folder structure as in above attached image, ** then how we will be able to download it from there ** – Taha Zubair Dec 08 '20 at 09:37
  • Did you perhaps consider renaming the files after they finished uploading? – mjwills Dec 08 '20 at 09:38
  • Total (05) files has been uploaded with a single upload attachment. I have checked rest of them as well, they are attached in image file. – Taha Zubair Dec 08 '20 at 09:44
  • 1
    Have you tried renaming the files after they finished uploading? – mjwills Dec 08 '20 at 09:48
  • yes If I tried to changes the extension after uploading, its work in that case, but I don't think like its a good practice.. – Taha Zubair Dec 08 '20 at 10:06
  • Why is it not good practice? – mjwills Dec 08 '20 at 13:02
  • Its mean we need to rename file when its upload successfully by custom code. – Taha Zubair Dec 09 '20 at 04:56
  • Awesome - and why is that bad practice? – mjwills Dec 09 '20 at 06:16

1 Answers1

0

After investigating with Tusdotnet I found only two options:

First Approach

Renaming of file when the file is successfully uploaded in the folder system. (As tusdotnet not maintain file extension when its upload successfully. enter image description here

Second Approach

Parallelly when tusdotnet upload your file successfully, upload same file with extension as well by using below code:

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

Reference:

enter image description here

Taha Zubair
  • 32
  • 3
  • 7