Exemplo n.º 1
0
        public static void UploadFiles(LuaTable info, LuaFunction call, float timeoutSec = 3f, int retry = 3)
        {
            var www = WWWTO.UploadFiles(WWWInfo.GetListByLua(info), null, call);

            www.TimeoutSec = timeoutSec;
            www.Retry      = retry;
            www.Start();
        }
Exemplo n.º 2
0
        /// <summary>
        /// 上传一个文件
        /// </summary>
        /// <param name="noticeKey">下载的key,用于多个上传组件时区分</param>
        /// <param name="info">上传的信息</param>
        /// <param name="timeoutSec">超时时间(秒)</param>
        /// <param name="del">上传进度回调
        /// (string noticeKey, double progress, int index, string msg);
        /// 上传key,上传进度(-1代表有错;1代表全部上传过[可能完成或部分失败]),当前上传的文件index,msg信息(当progress==1且msg=="succeeded"才表示上传成功)
        /// </param>
        /// <param name="lua">lua回调,参数跟del相同</param>
        public void UploadFile(string noticeKey, WWWInfo info, float timeoutSec, WWWRstDel del, LuaFunction lua)
        {
            mType   = WWWType.upload;
            mRstDel = del;
            setLuaCallback(lua);
            mTimeoutSec = timeoutSec;
            mTotalSize  = info.Size;
            NoticeKey   = noticeKey;

            mList.Clear();
            mList.Add(info);
        }
Exemplo n.º 3
0
        public static WWWTO UploadFile(WWWInfo info, WWWRstDel del, LuaFunction lua)
        {
            WWWTO www = new WWWTO();

            www.mType   = WWWType.upload;
            www.mRstDel = del;
            www._setLuaCallback(lua);
            www.mTotalSize = info.Size;

            www.mList.Clear();
            www.mList.Add(info);

            return(www);
        }
Exemplo n.º 4
0
        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);
            }
        }
Exemplo n.º 5
0
        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;
        }
Exemplo n.º 6
0
        public static void DownloadFiles(string noticeKey, LuaTable infos, float timeoutSec = 1f)
        {
            TimeOutWWW t = GameManager.Instance.gameObject.AddComponent <TimeOutWWW>();

            t.DownloadFiles(noticeKey, WWWInfo.GetListByLua(infos), timeoutSec, GameManager.Instance.OnLuaWWWRst, null);
        }