休息服务不返回大文件流 [英] Big file stream is not returned with rest service

查看:66
本文介绍了休息服务不返回大文件流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个REST GET API,它使用WCF库编写,以返回位于托管该Web服务应用程序的API服务器上的特定请求文件的Stream。如果请求的文件的大小很小,该服务运行良好;小于100 MB。但是如果文件大小大于> 100 MB,然后服务返回0字节没有任何记录信息我可以得到库方法(说,catch块)。



库方法(类库)项目)返回所需文件的流是



I have a REST GET API that is written using WCF library to return Stream of a specific requested file that is located on API server that hosts that web service application. The service works well if the size of the requested file is small; that is less than 100 MB. But if file size is greater than > 100 MB, then the service returns 0 bytes without any logged information I can get the library method (saying, the catch block).

The library method (the class library project) returns Stream of needed file is

public Stream GetFile(string fileId, string seekStartPosition=null)
        {
            _lastActionResult = string.Empty;
            Stream fileStream = null;

            try
            {
            Guid fileGuid;
            if (Guid.TryParse(fileId, out fileGuid) == false)
            {
                _lastActionResult = string.Format(ErrorMessage.FileIdInvalidT, fileId);
            }
            else
            {
                ContentPackageItemService contentItemService = new ContentPackageItemService();
                string filePath = DALCacheHelper.GetFilePath(fileId);

                if (File.Exists(filePath))
                {
                    fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);

                    long seekStart = 0;
                    // if seek position is specified, move the stream pointer to that location
                    if (string.IsNullOrEmpty(seekStartPosition) == false && long.TryParse(seekStartPosition, out seekStart))
                    {
                        // make sure seek position is smaller than file size
                        FileInfo fi = new FileInfo(filePath);
                        if (seekStart >= 0 && seekStart < fi.Length)
                        {
                            fileStream.Seek(seekStart, SeekOrigin.Begin);
                        }
                        else
                        {
                            _lastActionResult = string.Format(ErrorMessage.FileSeekInvalidT, seekStart, fi.Length);
                        }
                    }
                }
                else
                {
                    _lastActionResult = string.Format(ErrorMessage.FileNotFoundT, fileId);
                    Logger.Write(_lastActionResult,
                        "General", 1, Constants.LogId.RESTSync, System.Diagnostics.TraceEventType.Error, System.Reflection.MethodBase.GetCurrentMethod().Name);
                }

            }
      }
      catch(Exception ex)
      {
          Logger.Write(ex,"General", 1, Constants.LogId.RESTSync, System.Diagnostics.TraceEventType.Error, System.Reflection.MethodBase.GetCurrentMethod().Name);
      }

     return fileStream;

    }







API method on the client side project (where .svc file is):










[WebGet(UriTemplate = "files/{fileid}")]
        public Stream GetFile(string fileid)
        {
            ContentHandler handler = new ContentHandler();
            Stream fileStream = null;
            try
            {
                fileStream = handler.GetFile(fileid);
            }
            catch (Exception ex)
            {
                Logger.Write(string.Format("{0} {1}", ex.Message, ex.StackTrace), "General", 1, Constants.LogId.RESTSync, System.Diagnostics.TraceEventType.Error, System.Reflection.MethodBase.GetCurrentMethod().Name);

                throw new WebFaultException<ErrorResponse>(new ErrorResponse(HttpStatusCode.InternalServerError, ex.Message), HttpStatusCode.InternalServerError);
            }

            if (fileStream == null)
            {
                throw new WebFaultException<ErrorResponse>(new ErrorResponse(handler.LastActionResult), HttpStatusCode.InternalServerError);
            }

            return fileStream;

        }

推荐答案

读取无限流长度的方法是HttpRequest.GetBufferlessInputStream [ ^ ]。

The way to read an unlimited stream length is HttpRequest.GetBufferlessInputStream[^].
Quote:

如果请求正在上传大文件并且您希望在上载完成之前开始访问文件内容,则此方法非常有用。

This method can be useful if the request is uploading a large file and you want to begin accessing the file contents before the upload is finished.



或者你可能想要它(JavaScript解决方案)。



WCF 大数据流 [ ^ ]。

参考文章:如何从WCF服务下载大型文件? [ ^ ]



-KR


Or you might want to chunk it (JavaScript solution).

With WCF Large Data streaming[^] is possible.
Refer this article: How to Download a Large File from a WCF Service?[^]

-KR


这篇关于休息服务不返回大文件流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆