2017-02-18 139 views
0

我在将用户保存在本地文件夹中的文件(.mp3)上传到firebase时遇到问题。 这是怎样一个文件从本地文件夹检索:将本地文件夹中的文件上传到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文档中,文件应该作为文件流上传。从“资产”文件夹上传文件时这起作用,但不适用于本地文件夹中的文件。我尝试从MostRecentlyUsedList上传,但仍无法上传。任何想法为什么这是失败?

回答

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); 
+0

它的工作原理,但即使它从本地文件夹中获取文件仍然无法上传。我认为这与流媒体有关。 – Elisha

0

升压-Labs的C#火力地堡 - 存储API使用流文件上传。文件应该作为Stream上传。对我有效的是使用Memory Stream。

首先,我检索从本地文件夹中的文件:

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:

StorageFolder folder = ApplicationData.Current.LocalFolder; 

var songfolder = await folder.GetFolderAsync("Songs"); 

StorageFile mp3file = await songfolder.GetFileAsync(mp3fileforupload); 

然后我使用DataReader读取文件的字节

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; 

这样做。文件已上传。