Exemple #1
0
        private static void DownloadFile(string url)
        {
            WebRequest  WebRequestObject = HttpWebRequest.Create(url);
            WebResponse ResponseObject   = WebRequestObject.GetResponse();

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Headers.Add(ResponseObject.Headers);

            Stream       stream    = ResponseObject.GetResponseStream();
            MemoryStream stmMemory = new MemoryStream();

            byte[] buffer = new byte[64 * 1024];
            int    i;

            while ((i = stream.Read(buffer, 0, buffer.Length)) > 0)
            {
                stmMemory.Write(buffer, 0, i);
            }
            byte[] arraryByte = stmMemory.ToArray();
            stmMemory.Close();

            if (ResponseObject.ContentType.Contains("application/json"))
            {
                MediaFile.DownloadReturnValue retValue = JsonConvert.DeserializeObject <MediaFile.DownloadReturnValue>(System.Text.Encoding.UTF8.GetString(arraryByte));
                if (retValue.ErrorCode != 0)
                {
                    throw new Exception("获取媒体文件异常。ErrorCode=" + retValue.ErrorCode + ",ErrorMessage=" + retValue.ErrorMessage);
                }
            }

            HttpContext.Current.Response.OutputStream.Write(arraryByte, 0, arraryByte.Length);
        }
Exemple #2
0
        public static MediaFile.DownloadReturnValue DownloadData(string mediaId)
        {
            string      url = ServiceUrl.DownloadMediaFile_Temp + "?access_token=" + AccessToken.Value + "&media_id=" + mediaId;
            WebRequest  WebRequestObject = HttpWebRequest.Create(url);
            WebResponse ResponseObject   = WebRequestObject.GetResponse();

            MediaFile.DownloadReturnValue retValue = new MediaFile.DownloadReturnValue();

            retValue.FileSize = ResponseObject.ContentLength;
            foreach (var key in ResponseObject.Headers.AllKeys)
            {
                switch (key.ToLower())
                {
                case "content-disposition":
                    int      index = ResponseObject.Headers[key].IndexOf("filename=");
                    string[] a     = ResponseObject.Headers[key].Remove(0, index + 9).Split(new char[] { '"' }, StringSplitOptions.RemoveEmptyEntries);
                    if (a.Length > 0)
                    {
                        retValue.FileName = a[0];
                    }
                    break;

                case "content-type":
                    retValue.ContentType = ResponseObject.Headers[key];
                    break;

                default:
                    break;
                }
            }

            retValue.FileStream = ResponseObject.GetResponseStream();
            MemoryStream stmMemory = new MemoryStream();

            byte[] buffer = new byte[64 * 1024];
            int    i;

            while ((i = retValue.FileStream.Read(buffer, 0, buffer.Length)) > 0)
            {
                stmMemory.Write(buffer, 0, i);
            }
            retValue.FileData = stmMemory.ToArray();
            stmMemory.Close();

            if (ResponseObject.ContentType.Contains("application/json"))
            {
                MediaFile.DownloadReturnValue retValue2 = JsonConvert.DeserializeObject <MediaFile.DownloadReturnValue>(System.Text.Encoding.UTF8.GetString(retValue.FileData));
                if (retValue2.ErrorCode != 0)
                {
                    retValue = retValue2;
                }
            }

            return(retValue);
        }