file-upload - 无法获取 Blazor 文件上传以上传文件
问题描述
我有一个 Blazor 应用程序,我在其中使用此网站上的 BlazorInputFile - https://blog.stevensanderson.com/2019/09/13/blazor-inputfile/但是该页面仅将其加载到内存流中,而不是将文件复制到服务器上的文件夹。我需要将其复制到服务器上的文件夹中。
<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);
}
}
在上面的代码中,我创建了一个 UploadFile 方法并采用了我通常的上传文件的方式,但显然它不会工作,因为 IFileListEntry 没有 SaveAs 方法并且 Server 无法在 Blazor 上工作。
请问我如何最好地将这个文件上传到服务器?(UploadFile 方法将在表单提交时被调用)。
解决方案
推荐阅读
- php - 使用 XDebug 进行调试时,如何定义 PHP 对象在 VSCode 中的显示方式?
- python - 'numpy.float64' 类型的对象没有 len():我该如何解决这个问题?
- oculus - 如何在 Oculus Quest 浏览器中自动打开链接?
- r - 以向量化方式基于数据子集定义多行
- firebase - 从子集合 firestore + FLUTTER 中检索信息
- php - Laravel 关系不适用于 leftJoin
- regex - 带有可选下划线的下划线分隔表名的正则表达式
- hibernate - 如何在 hbm 文件中引用 java.time.LocalDate?
- javascript - 有没有办法通过节点运行 scrollreveal.js?
- python - 如何在 Discord.py 中检查消息是否包含表情符号?