FilesUploadFeature

Hi,
When using the FilesUploadFeature is there an example you can point me so that I can read the file?

I’m attempting to read the path provided to me via(UploadTo attribute) in a background job via IVirtualFiles but nothing gets returned.

Thanks

Typically uploaded files can only be read once, which the FileUploadFeature does when it saves the file and populates the DTO property with the location of the file, you should be able to read it from the uploaded file location from where you’ve configured it in the plugin location.

You could also use a Files Upload Transformer to intercept the Uploaded File and use InputStream.CopyToNewMemoryStreamAsync() to copy it to a new MemoryStream which can be reread, where your Service can re-read from Request.Files property. Check the linked docs for the example.

Sorry I should have been clearer, what I meant was after the file has been saved to disk and at a later date I want to be able to load/read the file in a service do I use the VirtualFiles or something else?

I thought getting the file with the path via VirtualFiles.GetFile would be enough but it doesn’t work.

This is my config

    public override void Configure()
    {
        TypeScriptGenerator.InsertTsNoCheck = true;

        SetConfig(new HostConfig());

        var azureBlobConnString = AppSettings.GetConnectionString("AzureBlob");
        var azureBlobVfs = new AzureBlobVirtualFiles(azureBlobConnString, "hirer");
        // Register Azure Blob VFS as the default IVirtualFiles
        Container.Register<IVirtualFiles>(azureBlobVfs);


        Plugins.Add(new FilesUploadFeature(
            new UploadLocation(FileLocations.HirerAzure, azureBlobVfs, readAccessRole: RoleNames.AllowAnyUser,
                writeAccessRole: RoleNames.AllowAnyUser, maxFileBytes: 10_000_000, resolvePath: ResolveUploadPath
            )));
    }

and this is how I am trying to read the file which I hardcoded for breavity

       var file = vfs.GetFile(@"/upload/HirerAzure/2/xxx.pdf");

You have to read it from whatever VFS you’ve configured to save it to, e.g. azureBlobVfs.

The upload location name is also included in the path. Here’s the implementation of the GetFileUpload API used to access uploaded files:

I’m trying to run this from a background task where I have no access to the Request and other information except the path, I’m struggling to get it working

When reporting issues please provide some context about your issue, what you’ve tried, what’s failing, any Exception StackTraces, etc. E.g. What path is the uploaded file saved to and what path are you trying to read it from?

If you know the path the file is saved to, you should be able to use it to get it from your VFS provider, e.g:

var file = vfs.GetFile(path);

First make sure you know what the right path is and you can use your VFS provider to get the file.

The (Azure) VFS provider can be accessed from any dependency you’ve registered it to.

Although if using .NET 8 + newer templates (e.g. with Endpoint Routing) all dependencies and plugins need to be registered in ConfigureServices, see the docs for an example of configuring the FilesUploadFeature plugin. i.e. you should not be using registering Plugins or Dependencies in Configure().

A reference to the VFS is also available from the Upload Location you’ve configured in you plugin where I expect you should be able to use it to access your file with something like:

var location = feature.AssertLocation(locationName);
var vfsPath = location.Name.CombineWith(path);
var file = location.VirtualFiles.GetFile(vfsPath);

This doesn’t require a IRequest context so it should be accessible from a background Thread.

Sure I’ll add more context in next time, it turns out it the configuration code was in the wrong place like you said.