private IEnumerator UploadFile(FileInfo file)
        {
            WWWForm form = new WWWForm();

            form.AddField("uploadType", "media");
            form.AddField("name", SystemInfoUtil.PackageName() + "/" + SystemInfoUtil.DeviceID() + "/" + file.Name);

            bool delete = false;

            using (UnityWebRequest request = new UnityWebRequest(uploadUrl + "?" + Encoding.UTF8.GetString(form.data)))
            {
                request.method = UnityWebRequest.kHttpVerbPOST;
                request.SetRequestHeader("Content-Type", "application/json");

                // set uploadHandler
                UploadHandlerFile uploadHandler = new UploadHandlerFile(file.FullName);
                request.uploadHandler = uploadHandler;

                yield return(request.SendWebRequest());

                if (request.isHttpError)
                {
                    LogEvent(EventType.Error, new Dictionary <string, object> {
                        { "msg", "Http error uploading analytics file: " + request.responseCode }
                    });
                }
                else if (request.isNetworkError)
                {
                    continueUpload = false;
                    LogEvent(EventType.Error, new Dictionary <string, object> {
                        { "msg", "Network error uploading analytics file!" }
                    });
                }
                else
                {
                    delete = true;
                    LogEvent("Analytics", new Dictionary <string, object> {
                        { "msg", "Uploaded file: " + file.Name }
                    });
                }
            }

            // wait 1 frame for request to have been cleaned up and released the file
            yield return(null);

            if (delete)
            {
                try
                {
                    file.Delete();
                }
                catch (Exception e)
                {
                    LogEvent(EventType.Error, new Dictionary <string, object> {
                        { "msg", "Deleting analytics file: " + file.Name + " failed: " + e.Message }
                    });
                }
            }
        }
Exemple #2
0
 // todo 1.Path 2.上传的data 好像也是看不见的,需要再服务器查看,查看 1、编码格式 2、数据对不对
 private void MyFileRequest()
 {
     using (UnityWebRequest request = new UnityWebRequest())
     {
         string        filePath = "D:/Desktop/1.txt";
         UploadHandler uploader = new UploadHandlerFile(filePath);
         uploader.contentType  = "application/octet-stream";
         request.uploadHandler = uploader;
     }
 }
Exemple #3
0
        IEnumerator UploadFile(string filePath, string name)
        {
            UploadHandler uploadHandler = new UploadHandlerFile(filePath);
            //Debug.Log(FirebaseConfig.storageEndpoint + GetAuthParam());
            RequestHelper req = new RequestHelper
            {
                Uri    = FirebaseConfig.storageEndpoint,
                Params = new Dictionary <string, string>
                {
                    { "uploadType", "media" },
                    { "name", string.IsNullOrEmpty(path)?name:$"{path}/{name}" }
                },
                ContentType   = "application/octet-stream",
                UploadHandler = uploadHandler
            };

            // Authenticate request if any user signed in
            if (FirebaseAuthentication.currentUser != null)
            {
                req.Headers.Add("Authorization", "Bearer " + FirebaseAuthentication.currentUser.accessToken);
            }


            RESTHelper.Post(req, res =>
            {
                UploadResponse uploadResponse = JsonUtility.FromJson <UploadResponse>(res);

                uploadResponse.downloadUrl = $"{FirebaseConfig.storageEndpoint}/{uploadResponse.name.Replace("/","%2F")}?alt=media&token={uploadResponse.downloadTokens}";

                callbackHandler.successCallback?.Invoke(uploadResponse);
            },
                            err =>
            {
                callbackHandler.exceptionCallback?.Invoke(err);
            });

            float lastProgress = 0f;

            //Progress Callback
            if (uploadProgressCallback != null)
            {
                while (uploadHandler.progress != 1)
                {
                    uploadProgressCallback.Invoke(uploadHandler.progress);
                    yield return(new WaitUntil(() =>
                                               uploadHandler.progress >= 0.95f? uploadHandler.progress == 1f : //If reached 95% or avobe, we will wait for 100%
                                               uploadHandler.progress >= lastProgress + 0.05f));               //Else, we will wait for reaching next 5%
                }
                uploadProgressCallback.Invoke(1f);
                uploadHandler.Dispose();
            }
        }