示例#1
0
        IEnumerator DownloadFileSizeHandler()
        {
            HeadHandler     handler = new HeadHandler();
            UnityWebRequest request = UnityWebRequest.Head(mDownloadObj.url);

            request.downloadHandler = handler;
            request.timeout         = UnityDownloadManager.TIMEOUT;
            request.chunkedTransfer = true;
            request.disposeDownloadHandlerOnDispose = true;
            yield return(request.SendWebRequest());

            //Dictionary<string, string> dic = request.GetResponseHeaders();
            //foreach (var item in dic) Debug.Log(item.Key + "     " + item.Value);
            //item.Key:Content-Type  item.Value:video/mp4
            //item.Key:Content-Disposition  item.Value:inline; filename = "o_1d6hoeipu1bem1rdjvqtush1gu8l.mp4"; filename *= utf - 8' 'o_1d6hoeipu1bem1rdjvqtush1gu8l.mp4

            //设置文件名的后缀
            string content_Type = request.GetResponseHeader("Content-Type");

            string[] arr     = content_Type.Split('/');
            string   houzhui = string.Format(".{0}", arr[arr.Length - 1]);

            if (!mDownloadObj.fileName.Contains("."))
            {
                mDownloadObj.fileName += houzhui;
                Debug.Log("添加后缀:" + mDownloadObj.fileName);
            }

            if (mDownloadObj.OnGetFileSizeAction != null)
            {
                mDownloadObj.SetContentLength(handler.ContentLength);
                mDownloadObj.OnGetFileSizeAction((int)request.responseCode, mDownloadObj);//request.responseCode == 200 代表获取文件大小成功
            }
            request.Abort();
            request.Dispose();
        }
 /// <summary>
 /// 文件数据长度
 /// </summary>
 protected override void ReceiveContentLength(int contentLength)
 {
     mDownloadObj.SetContentLength(mDownloadObj.sDownloadFileResult.downloadedLength + contentLength);
 }