2017-03-20 66 views
1

我需要我的照片上传到我的本地路径火力存储,但我不断收到错误上传本地图片火力地堡存储(Unity3D)

System.AggregateException: Exception of type 'System.AggregateException' was thrown. 

Firebase.Storage.StorageException: An unknown error occurred, please check the HTTP result code and inner exception for server response. ---> System.IO.FileNotFoundException: Could not find file "C:\Users\jorren\AppData\LocalLow\DefaultCompany\JKL\636256043481512729.jpg". 
File name: 'C:\Users\jorren\AppData\LocalLow\DefaultCompany\JKL\636256043481512729.jpg' 
    at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00209] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/FileStream.cs:305 
    at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share) [0x00000] in <filename unknown>:0 
    at (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare) 
    at System.IO.File.OpenRead (System.String path) [0x00000] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/File.cs:363 
    at Firebase.Storage.UploadTask..ctor (Firebase.Storage.StorageReference targetRef, Firebase.Storage.StorageMetadata metadata, System.String file, System.Uri existingUploadUri, System.Threading.Tasks.TaskCompletionSource`1 userTask, IProgress`1 progress, CancellationToken token, System.Threading.SynchronizationContext ctx) [0x00000] in <filename unknown>:0 
    --- End of inner exception stack trace --- 

UnityEngine.Debug:Log(Object) 
Kudan.AR.Samples.<uploadToStorage>c__Iterator1:MoveNext() (at Assets/KudanAR/Samples/Scripts/SampleApp.cs:122) 
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr) 

这是我的代码:

Firebase.Storage.FirebaseStorage storage = Firebase.Storage.FirebaseStorage.DefaultInstance; 
     Firebase.Storage.StorageMetadata metadata; 
     // Create a storage reference from our storage service 
     Firebase.Storage.StorageReference storage_ref = storage.GetReferenceFromUrl("gs://sylpauljoyce-d9115.appspot.com"); 
     Firebase.Storage.StorageReference rivers_ref = storage_ref.Child("images/" + filename); 
     string fileContents = Application.persistentDataPath + "/" + filename; 
     var task = rivers_ref.PutFileAsync(fileContents); 

     yield return new WaitUntil(() => task.IsCompleted); 
     if (task.IsFaulted) { 
      Debug.Log(task.Exception.ToString()); 
     } else { 
      fileContents = ""; 
      Debug.Log("Finished uploading... Download Url: " + task.Result.DownloadUrl.ToString()); 
      url = task.Result.DownloadUrl.ToString(); 

      Debug.Log (url); 
     } 

我尝试使用PutBytesAsync而不是putFileAsync,但没有得到错误,但上传的文件类型是应用程序/八位字节流

我非常需要帮助。谢谢!

回答

0

好像这里有两个问题:

  1. 最初的错误是从Could not find file "C:\Users\jorren\AppData\LocalLow\DefaultCompany\JKL\636256043481512729.jpg"。你是否检查过这个文件存在于磁盘上?好像不能找到:(
  2. 内容类型由文件元数据设置,其默认值为application/octet-stream如果我们不能找到它。对磁盘上的文件,我们经常可以挑选内容类型脱档,但对内存中的数据,我们不能简单地设置内容类型的元数据(每the docs

例:

// Create file metadata including the content type 
var new_metadata = new Firebase.Storage.MetadataChange(); 
new_metadata.ContentType = "image/jpeg"; 

// Upload data and metadata 
mountains_ref.PutBytesAsync(custom_bytes, new_metadata, ...)