File upload 无法将Blazor文件上载到上载文件

File upload 无法将Blazor文件上载到上载文件,file-upload,blazor,File Upload,Blazor,我有一个Blazor应用程序,我使用这个网站上的BlazorInputFile,但是页面只将其加载到内存流中,而不是将文件复制到服务器上的文件夹中。我需要它复制到服务器上的文件夹 <div class="form-group"> <label for="taskName">Feature Image</label> <InputFile OnChange="HandleFileSelected" /> </div> @

我有一个Blazor应用程序,我使用这个网站上的BlazorInputFile,但是页面只将其加载到内存流中,而不是将文件复制到服务器上的文件夹中。我需要它复制到服务器上的文件夹

<div class="form-group">
    <label for="taskName">Feature Image</label>
    <InputFile OnChange="HandleFileSelected" />
</div>

@code {

IFileListEntry file;

void HandleFileSelected(IFileListEntry[] files)
{
    file = files.FirstOrDefault();
}

async Task CountLines()
{
    numLines = 0;
    using (var reader = new System.IO.StreamReader(file.Data))
    {
        while (await reader.ReadLineAsync() != null)
        {
            numLines++;
        }
    }
}

async Task UploadFile()
{
    if (file != null)
    {
        var path = System.IO.Path.Combine(Server.MapPath("~/Uploads/"));
        string pathstring = System.IO.Path.Combine(path.ToString());
        string filename1 = Guid.NewGuid() + System.IO.Path.GetExtension(file.Name);
        bool isexists = System.IO.Directory.Exists(path);
        if (!isexists)
        {
            System.IO.Directory.CreateDirectory(pathstring);
        }
        string uploadpath = pathstring + "\\" + filename1;
        file.SaveAs(uploadpath);
    }
}

特征图像
@代码{
IFileListEntry文件;
已选择void handlefiles(IFileListEntry[]文件)
{
file=files.FirstOrDefault();
}
异步任务计数行()
{
numLines=0;
使用(var reader=new System.IO.StreamReader(file.Data))
{
while(等待reader.ReadLineAsync()!=null)
{
numLines++;
}
}
}
异步任务上载文件()
{
如果(文件!=null)
{
var path=System.IO.path.Combine(Server.MapPath(“~/Uploads/”);
string pathstring=System.IO.Path.Combine(Path.ToString());
字符串filename1=Guid.NewGuid()+System.IO.Path.GetExtension(file.Name);
bool isexists=System.IO.Directory.Exists(路径);
如果(!isexists)
{
System.IO.Directory.CreateDirectory(路径字符串);
}
字符串上传路径=路径字符串+“\\”+文件名1;
file.SaveAs(上传路径);
}
}
在上面的代码中,我创建了一个UploadFile方法,并采用了我通常的上传文件的方式,但显然它不起作用,因为IFileListEntry没有SaveAs方法,服务器将无法在Blazor上工作


我怎样才能最好地将这个文件上传到服务器?(上传文件方法将在表单提交时调用).

这里回答了您的问题。这是Steve Anderson创建的示例代码的扩展。@enet我现在正尝试实现它。在使用HttpContext.Request.Form.Files的地方。当查看表单部件时,如果出现异常“Form=函数求值需要运行所有线程”。