Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
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):



C#
[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;

        }
Posted

1 solution

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.

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900