Beispiel #1
0
        public async Task <IActionResult> BreakpointDownload(string name)
        {
            //先获取文件大小
            var    newName    = Guid.NewGuid() + "-" + name;
            long   fileLength = AliyunOSSHepler.GetInstance().GetFileLength(name);
            string mimeType   = MimeHelper.GetMimeMapping(name);
            int    partSize   = 1024 * 1024 * 10;
            var    partCount  = AliyunOSSHepler.GetInstance().CalPartCount(fileLength, partSize);

            for (var i = 0; i < partCount; i++)
            {
                var startPos = partSize * i;
                var endPos   = partSize * i + (partSize < (fileLength - startPos) ? partSize : (fileLength - startPos)) - 1;
                AliyunOSSHepler.GetInstance().Download(downloadPath, startPos, endPos, name, newName);
            }
            var file = new byte[fileLength];

            using (var fileStream = new FileStream(downloadPath + newName, FileMode.Open))
            {
                fileStream.Read(file, 0, file.Length);
            }

            System.IO.File.Delete(downloadPath + newName);
            return(File(file, mimeType, name));
        }
Beispiel #2
0
        public async Task <ResultContract <byte[]> > GetBreakpoint(string name, long startPos, long endPos)
        {
            var result = new ResultContract <byte[]>()
            {
                Code = 0, Msg = "获取成功"
            };

            //先获取文件大小
            try
            {
                var    newName    = Guid.NewGuid() + "-" + name;
                long   fileLength = AliyunOSSHepler.GetInstance().GetFileLength(name);
                string mimeType   = MimeHelper.GetMimeMapping(name);
                AliyunOSSHepler.GetInstance().Download(downloadPath, startPos, endPos, name, newName);
                var file = new byte[endPos - startPos];
                using (var fileStream = new FileStream(downloadPath + newName, FileMode.Open))
                {
                    fileStream.Read(file, 0, file.Length);
                }

                System.IO.File.Delete(downloadPath + newName);

                result.Data = file;
            }
            catch (Exception e)
            {
                result.Msg  = "获取失败";
                result.Code = -1;
            }

            return(result);
        }
Beispiel #3
0
        public async Task <IActionResult> DownloadImg(string imgurl)
        {
            string          filename       = Path.GetFileName(imgurl);
            string          mimeType       = MimeHelper.GetMimeMapping(imgurl);
            HttpWebRequest  request        = (HttpWebRequest)WebRequest.Create(imgurl);
            HttpWebResponse response       = request.GetResponse() as HttpWebResponse;
            Stream          responseStream = response.GetResponseStream();

            return(File(responseStream, mimeType, filename));
        }