예제 #1
0
        public UploadAndDownloadFile DownloadFile(DownloadRequest request)
        {
            UploadAndDownloadFile result = new UploadAndDownloadFile();
            try
            {
                string filePath = request.FilePath;
                FileInfo fileInfo = new FileInfo(filePath);

                if (!fileInfo.Exists)
                {
                    throw new FileNotFoundException("File not found", request.FilePath);
                }

                // 注意:此处不能使用上面的方式进行
                // 因为流在客户端接收完成前,不能够关闭
                // 所以此处将对象放在了内存中
                result.FilePath = request.FilePath;
                result.Length = fileInfo.Length;
                _fs = File.OpenRead(filePath);
                result.FileByteStream = _fs;

            }
            catch (UnauthorizedAccessException ex)
            {
                throw new Exception("Download File Error", ex);
            }

            return result;
        }
예제 #2
0
        public void UploadFile(UploadAndDownloadFile request)
        {
            using (FileStream targetStream = File.OpenWrite(request.FilePath))
            {
                // 推荐写法
                // 分片拷贝字节流
                request.FileByteStream.CopyTo(targetStream, 4096);

                // 与上面的效果一致
                /*
                int size = 1024 * 4;
                byte[] buf = new byte[size];
                size = request.FileByteStream.Read(buf, 0, size);
                while (size > 0)
                {
                    targetStream.Write(buf, 0, size);
                    size = request.FileByteStream.Read(buf, 0, size);
                }
                **/
            }
        }
예제 #3
0
 private void Upload()
 {
     if (FileUpload_Main.HasFile)
     {
         TransferServiceClient clientUpload = new TransferServiceClient("basicHttpUpload");
         UploadAndDownloadFile uploadRequestInfo = new UploadAndDownloadFile
         {
             FilePath = Path.Combine(Server.MapPath("~/TempFiles"), FileUpload_Main.FileName),
             Length = FileUpload_Main.PostedFile.InputStream.Length,
             FileByteStream = FileUpload_Main.PostedFile.InputStream
         };
         clientUpload.UploadFile(uploadRequestInfo);
     }
 }