1

我需要能够提取完整的文件名,包括用户使用我的 InputFile 元素选择文件时的路径。

所以,作为一个例子,使用这个

<InputFile OnChange="FileSelected" />

我可以像这样在事件处理程序中看到文件名

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

但是经过各种谷歌搜索尝试,我无法弄清楚如何获取完整的文件名。

这里的目的是向用户展示一个文件对话框,他们可以在其中选择一个文件,然后我可以使用完整文件路径加载一些其他需要的文件。

谢谢

4

2 回答 2

2

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.

于 2021-08-01T12:51:58.183 回答
-1

尝试这个....

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; 
    } 
} 
于 2021-08-01T12:06:07.267 回答