我无法将用户存储在本地文件夹中的文件(.mp3)上传到firebase。 这是从本地文件夹中检索文件的方式:
StorageFolder folder = ApplicationData.Current.LocalFolder;
var songfolder = await folder.GetFolderAsync("Songs");
StorageFile mp3file = await songfolder.GetFileAsync(mp3fileforupload);
这就是我创建文件的流文件并上传的方式:
var stream = File.Open(mp3file.Path, FileMode.Open);
var task = new FirebaseStorage("-my-bucket-.appspot.com")
.Child("songs")
.Child(new_song_id)
.PutAsync(stream);
task.Progress.ProgressChanged += (s, f) => uploadProgress(f.Percentage);
var downloadurl = await task;
Debug.WriteLine("DOWNLOAD_URL " + downloadurl);
文件无法上传。从Step-up-labs文档中,文件应作为文件流上传。这在从Assets文件夹上载文件时起作用,但不适用于本地文件夹中的文件。我尝试从MostRecentlyUsedList上传但仍无法上传。知道为什么会失败吗?
答案 0 :(得分:0)
尝试使用此功能打开文件
Windows.Storage.StorageFolder storageFolder =
Windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Storage.StorageFile sampleFile =
await storageFolder.GetFileAsync(mp3file.Path);
var stream = await sampleFile.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
答案 1 :(得分:0)
Step-Up-Labs C#Firebase-Storage API使用Stream进行文件上传。文件应作为Stream上传。对我有用的是使用记忆流。
首先,我从本地文件夹中检索文件:
StorageFolder folder = ApplicationData.Current.LocalFolder;
var songfolder = await folder.GetFolderAsync("Songs");
StorageFile mp3file = await songfolder.GetFileAsync(mp3fileforupload);
然后我使用DataReader读取文件的字节:
byte[] fileBytes = null;
using (IRandomAccessStreamWithContentType stream = await mp3file.OpenReadAsync())
{
fileBytes = new byte[stream.Size];
using (DataReader reader = new DataReader(stream))
{
await reader.LoadAsync((uint)stream.Size);
reader.ReadBytes(fileBytes);
}
}
然后我使用MemoryStream进行上传:
Stream stream = new MemoryStream(fileBytes);
var task = new FirebaseStorage("-my-bucket-.appspot.com")
.Child("songs")
.Child(new_song_id)
.PutAsync(stream);
task.Progress.ProgressChanged += (s, f) => uploadProgress(f.Percentage);
var downloadurl = await task;
这就是诀窍。文件已上传。