休息服务不返回大文件流

编程入门 行业动态 更新时间:2024-10-22 12:34:13
本文介绍了休息服务不返回大文件流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个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

更多推荐

休息服务不返回大文件流

本文发布于:2023-11-25 17:39:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630712.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:大文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!