在我的Blazor应用程序中,我具有要上传图像的文件夹wwwroot / upload。但是图像永远不会进入目录。
我的代码-
xcode://
为什么文件没有到达目的地?
更新
我的api / Blog / UploadFeaturesImage在下面,我相信问题就在这里。其将所有文件上载到API项目上的名为Upload的文件夹中。我该如何告诉它上传到wwwroot / upload?
async Task UploadFile()
{
if (file != null)
{
// Just load into .NET memory to show it can be done
// Alternatively it could be saved to disk, or parsed in memory, or similar
var ms = new System.IO.MemoryStream();
await file.Data.CopyToAsync(ms);
status = $"Finished loading {file.Size} bytes from {file.Name}";
// TODO: Need to upload the file to wwwroot/upload
var content = new MultipartFormDataContent {
{ new ByteArrayContent(ms.GetBuffer()), "\\upload\\", file.Name }
};
await Http.PostAsync("api/Blog/UploadFeaturedImage", content);
}
}
答案 0 :(得分:0)
不确定这是否对您有帮助,但是我有一个名为BlazorFileUpload的Nuget程序包,它带有一个示例项目:
https://github.com/DataJuggler/BlazorFileUpload
这基于Microsoft Blazor的创建者SteveSanderson的BlazorInputFile。
剃刀代码:
@使用DataJuggler.Blazor.FileUpload
<FileUpload CustomSuccessMessage="Your file uploaded successfully."
OnChange="OnFileUploaded" FilterByExtension="true"
ShowStatus="false" PartialGuidLength="10" MaxFileSize="4194304"
ShowCustomButton="true" ButtonText="Select File"
CustomButtonClassName="buttonwide"
AllowedExtensions=".jpg;.png;" ShowResetButton="true" OnReset="OnReset"
ResetButtonClassName="button" ResetButtonText="Reset"
CustomExtensionMessage="Only .jpg and .png files are allowed."
AppendPartialGuid="true"
FileTooLargeMessage="The file uploaded must be 4 megabytes or smaller.">
</FileUpload>
上传文件后,将调用OnFileUploaded事件回调:
/// <summary>
/// method returns the File Uploaded
/// </summary>
private void OnFileUploaded(UploadedFileInfo uploadedFileInfo)
{
// if aborted
if (uploadedFileInfo.Aborted)
{
// get the status
status = uploadedFileInfo.ErrorMessage;
}
else
{
// get the status
status = "The file " + uploadedFileInfo.FullName + " was uploaded.";
// other information about the file is available
//DateTime lastModified = uploadedFileInfo.LastModified;
//string nameAsItIsOnDisk = uploadedFileInfo.NameWithPartialGuid;
//string partialGuid = uploadedFileInfo.PartialGuid;
//long size = uploadedFileInfo.Size;
//string type = uploadedFileInfo.Type;
// The above information can be used to display, and / or process a file
}
}
示例项目位于上面列出的Git Repo的Samples文件夹中。
FileUpload组件默认的UploadDirectory为'wwwroot / Upload',因此它应该对您有用:
[Parameter]
public string UploadFolder { get; set; } = "wwwroot/Upload/";
我几乎用BlazorImageGallery示例完成了,应该在本周晚些时候完成:
如果有人要观看,BlazorFileUpload项目还提供了一个视频:https://youtu.be/Hj_k90mifSQ
也许会帮助某人。