示例#1
0
        public Stream DownloadFileStream(string serverfile, long offset = 0, long count = 0)
        {
            CusFileInfo file = new CusFileInfo();

            file.filepath = ConfigurationManager.AppSettings["ServerDownLoadURL"] + serverfile;
            file.FileName = serverfile;

            //string[] strlist1 = Directory.GetFiles(ConfigurationManager.AppSettings["ServerDownLoadURL"]);
            FileInfo ff = new FileInfo(file.filepath);

            if (File.Exists(file.filepath))
            {
                if (count == 0)
                {
                    count = ff.Length - offset;
                }
                var outgoingResponse = WebOperationContext.Current.OutgoingResponse;
                outgoingResponse.ContentType = "application/force-download";

                Console.WriteLine("开始下载:" + serverfile + "(" + offset + "--" + count + ")");
                CusStreamReader fs = new CusStreamReader(new FileStream(file.filepath, FileMode.Open, FileAccess.Read, FileShare.Read), offset, count);
                fs.Reading += (t) =>
                {
                    //限速代码,实际使用时可以去掉,或者精确控制
                    Thread.Sleep(10);
                    Console.WriteLine(t);
                };

                return(fs);
            }
            else
            {
                throw new FaultException("没找到文件:" + serverfile);
            }
        }
示例#2
0
        public Stream DownloadFileStream(string serverfile)
        {
            CusFileInfo file = new CusFileInfo();

            file.filepath = ConfigurationManager.AppSettings["ServerDownLoadURL"] + serverfile;

            if (File.Exists(file.filepath))
            {
                var  incomingRequest = WebOperationContext.Current.IncomingRequest;
                var  outgoingResponse = WebOperationContext.Current.OutgoingResponse;
                long offset = 0, count = file.FileLength;
                if (incomingRequest.Headers.AllKeys.Contains("Range"))
                {
                    var match = System.Text.RegularExpressions.Regex.Match(incomingRequest.Headers["Range"], @"(?<=bytes\b*=)(\d*)-(\d*)");
                    if (match.Success)
                    {
                        outgoingResponse.StatusCode = System.Net.HttpStatusCode.PartialContent;
                        string v1 = match.Groups[1].Value;
                        string v2 = match.Groups[2].Value;
                        if (!match.NextMatch().Success)
                        {
                            if (v1 == "" && v2 != "")
                            {
                                var r2 = long.Parse(v2);
                                offset = count - r2;
                                count  = r2;
                            }
                            else if (v1 != "" && v2 == "")
                            {
                                var r1 = long.Parse(v1);
                                offset = r1;
                                count -= r1;
                            }
                            else if (v1 != "" && v2 != "")
                            {
                                var r1 = long.Parse(v1);
                                var r2 = long.Parse(v2);
                                offset = r1;
                                count -= r2 - r1 + 1;
                            }
                            else
                            {
                                outgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;
                            }
                        }
                    }
                }
                outgoingResponse.ContentType   = "application/force-download";
                outgoingResponse.ContentLength = count;

                Console.WriteLine("开始下载:" + serverfile + "(" + offset + "--" + count + ")");
                CusStreamReader fs = new CusStreamReader(new FileStream(file.filepath, FileMode.Open, FileAccess.Read, FileShare.Read), offset, count);
                //fs.Reading += (t) =>
                //    {
                //        //限速代码,实际使用时可以去掉,或者精确控制
                //        Thread.Sleep(300);
                //        Console.WriteLine(t);
                //    };
                return(fs);
            }
            else
            {
                throw new FaultException("没找到文件:" + serverfile);
            }
        }