//上传data(post方式) public void PostUpload(string httpUrl, byte[] byteData, string remoteFile, delegateOnUpFinsh callback) { StartCoroutine(PostUploadAysnc(httpUrl, byteData, remoteFile, callback)); }
IEnumerator PostUploadAysnc(string httpUrl, byte[] byteData, string remoteFile, delegateOnUpFinsh callback) { WWWForm dataForm = new WWWForm(); dataForm.AddBinaryData("file", byteData, remoteFile); dataForm.AddField("fileName", remoteFile); var www = UnityWebRequest.Post(httpUrl, dataForm); yield return(www.Send()); if (www.isError) { www.Dispose(); yield return(new WaitForSeconds(0.5f)); www = UnityWebRequest.Post(httpUrl, dataForm); yield return(www.Send()); } if (www.isError) { www.Dispose(); yield return(new WaitForSeconds(0.5f)); www = UnityWebRequest.Post(httpUrl, dataForm); yield return(www.Send()); } if (www.isDone) { if (!www.isError) { if (callback != null) { callback(0, www.downloadHandler.text); } } else { if (callback != null) { callback(1, ""); } } www.Dispose(); } }
/// <summary> /// 上传file(post方式) /// </summary> /// <param name="httpUrl">上传地址</param> /// <param name="fileAbsolutePath">本地文件地址</param> /// <param name="remoteFile">远端文件名</param> /// <param name="callback">上传完成回调</param> public void PostUpload(string httpUrl, string fileAbsolutePath, string remoteFile, delegateOnUpFinsh callback) { if (System.IO.File.Exists(fileAbsolutePath)) { byte[] byteFile = System.IO.File.ReadAllBytes(fileAbsolutePath); PostUpload(httpUrl, byteFile, remoteFile, callback); } }