LePrinceDeDhump
LePrinceDeDhump

Reputation: 486

How to obtain the full file path when using InputFile in Blazor Server?

I need to be able to extract the full file name, including the path when the user selects a file using my InputFile element.

So, as an example, using this

<InputFile OnChange="FileSelected" />

I can see the filename in the event handler like so

void FileSelected(InputFileChangeEventArgs eventArgs) {

//eventArgs.File.Name has just the name of the file, e.g. ABC.csv but I need the full path like c:\userfolder\ABC.csv

but after various googling attempts, I haven't been able to figure out how to get the full file name.

The purpose here is to present the user with a file dialog box where they could pick a file and then I could load a few other files that are needed using the full file path.

Thanks

Upvotes: 2

Views: 11996

Answers (2)

David
David

Reputation: 218808

then I could load a few other files that are needed using the full file path

Nope.

The server cannot read from the client’s file system. Any files that need to be sent to the server, the client needs to send them.

Even the client-side code is very restricted by the browser’s sandboxed environment. The user needs to supply the file in order to grant permission. See: https://developer.mozilla.org/en-US/docs/Web/API/File

You’ll likely need to re-think the use case. Because browsers specifically don’t allow what you want to do.

Upvotes: 4

Itz Moosa E. Mohd
Itz Moosa E. Mohd

Reputation: 41

try this....

public void OnChangeUpload(UploadChangeEventArgs args) 
{ 
    foreach (var file in args.Files) 
    { 
        var path = Path.GetFullPath("wwwroot\\Images\\") + file.FileInfo.Name; 
        FileStream filestream = new FileStream(path, FileMode.Create, FileAccess.Write); 
        file.Stream.WriteTo(filestream); 
        filestream.Close(); 
        file.Stream.Close(); 
        pathUrl = path; 
    } 
} 

Upvotes: 3

Related Questions