private IEnumerator _upOrDownload() { for (int i = 0; i < mList.Count; i++) { WWWInfo info = mList[i]; mRetryCount = 0; do { if (string.IsNullOrEmpty(info.Url) || string.IsNullOrEmpty(info.TargetPath)) { mFialedList.Add(info); _callback(-1d, "url/target is null or empty. url:" + info.Url + "; target:" + info.TargetPath); break; } if (WWWType.download == mType) { mWWW = new WWW(info.Url); } if (WWWType.upload == mType) { WWWUploadInfo uploadInfo = info as WWWUploadInfo; string path = uploadInfo.TargetPath; if (!string.IsNullOrEmpty(path) && Tools.CheckFileExists(path)) { WWW file = new WWW(Tools.GetUrlPathWriteabble(path)); while (!file.isDone) { yield return(null); } if (string.IsNullOrEmpty(file.error)) { //WWWForm是一个辅助类,该类用于生成表单数据, //然后WWW类就可以将该表单数据post到web服务器上了 WWWForm form = new WWWForm(); //添加二进制文件到表单,使用该函数可以上传文件或者图片到Web服务器 form.AddBinaryData(uploadInfo.FieldName, file.bytes, uploadInfo.SaveName, uploadInfo.MimeType); mWWW = new WWW(uploadInfo.Url, form); } } else { mFialedList.Add(info); _callback(-1d, "上传文件[" + path + "]不存在"); break; } } yield return(_checkWWWTimeout(TimeoutSec)); if (mIsTimeOut || !string.IsNullOrEmpty(mWWW.error)) { mWWW.Dispose(); mWWW = null; if (mRetryCount < Retry) { ++mRetryCount; continue; } else { mFialedList.Add(info); _callback(-1d, "超时或失失败;" + mWWW.error); } } else { mDoneSize += info.Size; mDoneCount += 1; if (WWWType.download == mType) { //下载完成,拷贝到目标位置 string savePath = info.TargetPath; if (!string.IsNullOrEmpty(savePath)) { if (File.Exists(savePath)) { File.Delete(savePath); } Tools.CheckDirExists(Directory.GetParent(savePath).ToString(), true); FileStream fsDes = File.Create(savePath); //TODO:优化,拷贝大文件时适当yield return fsDes.Write(mWWW.bytes, 0, mWWW.bytes.Length); fsDes.Flush(); fsDes.Close(); } } _callback(_getProgress(0), STR_DONE); mWWW.Dispose(); mWWW = null; break; } } while (mRetryCount <= Retry); } yield return(null); if (mFialedList.Count == 0) { _callback(1d, STR_SUCCEEDED); } else { _callback(1d, STR_FAILED); } yield break; }
private IEnumerator www() { for (int i = 0; i < mList.Count; i++) { WWWInfo info = mList[i]; if (string.IsNullOrEmpty(info.Url) || string.IsNullOrEmpty(info.TargetPath)) { callback(-1f, "url/target is null or empty "); continue; } if (WWWType.download == mType) { mWWW = new WWW(info.Url); } if (WWWType.upload == mType) { WWWUploadInfo uploadInfo = info as WWWUploadInfo; string path = uploadInfo.TargetPath; if (!string.IsNullOrEmpty(path) && Tools.CheckFileExists(path)) { WWW file = new WWW(Tools.GetUrlPathWriteabble(path)); while (!file.isDone) { yield return(null); } if (string.IsNullOrEmpty(file.error)) { //WWWForm是一个辅助类,该类用于生成表单数据, //然后WWW类就可以将该表单数据post到web服务器上了 WWWForm form = new WWWForm(); //添加二进制文件到表单,使用该函数可以上传文件或者图片到Web服务器 form.AddBinaryData(uploadInfo.FieldName, file.bytes, uploadInfo.SaveName, uploadInfo.MimeType); mWWW = new WWW(uploadInfo.Url, form); } } } if (null == mWWW) { callback(-1f, "www is null"); continue; } yield return(checkWWWTimeout(mTimeoutSec)); if (mIsTimeOut) { mWWW.Dispose(); mWWW = null; callback(-1f, "time out"); mFialedList.Add(info); } else { if (mWWW.isDone) { if (!string.IsNullOrEmpty(mWWW.error)) { //WWW上传或下载失败 mWWW.Dispose(); mWWW = null; callback(-1f, mWWW.error); mFialedList.Add(info); } else { mDoneSize += info.Size; mDoneCount += 1; if (WWWType.download == mType) { //下载完成,拷贝到目标位置 string savePath = info.TargetPath; if (!string.IsNullOrEmpty(savePath)) { if (File.Exists(savePath)) { File.Delete(savePath); } Tools.CheckDirExists(Directory.GetParent(savePath).ToString(), true); FileStream fsDes = File.Create(savePath); //TODO:优化,拷贝大文件时适当yield return fsDes.Write(mWWW.bytes, 0, mWWW.bytes.Length); fsDes.Flush(); fsDes.Close(); } } callback(getProgress(0), STR_DONE); } } mWWW.Dispose(); mWWW = null; } } yield return(null); if (mFialedList.Count == 0) { callback(1, STR_SUCCEEDED); } else { callback(1, STR_FAILED); } }
public static void UploadFiles(string noticeKey, LuaTable infos, float timeoutSec = 1f) { TimeOutWWW t = GameManager.Instance.gameObject.AddComponent <TimeOutWWW>(); t.UploadFiles(noticeKey, WWWUploadInfo.GetListByLua(infos), timeoutSec, GameManager.Instance.OnLuaWWWRst, null); }