在finally块C#中关闭文件流后不返回

时间:2014-11-14 10:14:29

标签: c# .net wcf rest

我通过使用WCF休息服务返回如下所示的流来下载文件。

 Stream stream = null;          
            var directoryInformation = CommonServices.Utility.DirectoryHelper.GetTempFolderRootLocation();
            string newFolderPath = directoryInformation + "\\Attachments\\" + Guid.NewGuid();
            try
            {

                Directory.CreateDirectory(newFolderPath);
                DataSet getDocumentContent = GetDocumentContent(engagementId, documentId);
                var fileName = getDocumentContent.Tables[0].Rows[0]["Fullname"] as string;
                var byteData= getDocumentContent.Tables[0].Rows[0]["FilestreamContent"] as byte[];
                string fullPath = newFolderPath + "\\" + fileName;        
                using (var fileStream = new FileStream(fullPath, FileMode.Create))
                {
                    if (byteData != null)
                    {
                        fileStream.Write(byteData,0,byteData.Length);
                        fileStream.Close();
                    }

                    if (WebOperationContext.Current != null)
                    {
                        WebOperationContext.Current.OutgoingResponse.ContentType = "application/octet-stream";
                        WebOperationContext.Current.OutgoingResponse.Headers.Add("content-disposition","inline; filename=" + fileName);
                    }
                }
                stream = File.OpenRead(fullPath);
                return stream;
            }
            catch (Exception exception)
            {

                return null;
            }

以上代码运行良好,可以在浏览器中下载文件。但是我必须在返回流后删除该文件。所以我试图关闭并删除包含finally块中的目录的文件,如下所示

   finally
            {
                if (stream != null) stream.Close();
                Directory.Delete(newFolderPath, true);
            } 

完整方法代码

 public Stream DownloadAttachment(string engagementId, string documentId)
        {
            Stream stream = null;          
            var directoryInformation = CommonServices.Utility.DirectoryHelper.GetTempFolderRootLocation();
            string newFolderPath = directoryInformation + "\\Attachments\\" + Guid.NewGuid();
            try
            {

                Directory.CreateDirectory(newFolderPath);
                DataSet getDocumentContent = GetDocumentContent(engagementId, documentId);
                var fileName = getDocumentContent.Tables[0].Rows[0]["Fullname"] as string;
                var byteData= getDocumentContent.Tables[0].Rows[0]["FilestreamContent"] as byte[];
                string fullPath = newFolderPath + "\\" + fileName;        
                using (var fileStream = new FileStream(fullPath, FileMode.Create))
                {
                    if (byteData != null)
                    {
                        fileStream.Write(byteData,0,byteData.Length);
                        fileStream.Close();
                    }

                    if (WebOperationContext.Current != null)
                    {
                        WebOperationContext.Current.OutgoingResponse.ContentType = "application/octet-stream";
                        WebOperationContext.Current.OutgoingResponse.Headers.Add("content-disposition","inline; filename=" + fileName);
                    }
                }
                stream = File.OpenRead(fullPath);
                return stream;
            }
            catch (Exception exception)
            {

                return null;
            }
           finally
            {
                if (stream != null) stream.Close();
                Directory.Delete(newFolderPath, true);
            }
        }

添加此代码文件后未在客户端下载。有没有办法删除该文件?请帮我解决此问题

2 个答案:

答案 0 :(得分:1)

您可以删除OperationContext.OperationCompleted中的文件,如http://devdump.wordpress.com/2008/12/07/disposing-return-values/

所示
OperationContext clientContext = OperationContext.Current;
clientContext.OperationCompleted += new EventHandler(delegate(object sender, EventArgs args)
   {
          if (stream != null) stream.Close();
          Directory.Delete(newFolderPath, true);
   });

答案 1 :(得分:0)

将您的方法更改为此。

public void DownloadAttachment(string engagementId, string documentId, Action<Stream> processFile)

而不是返回流调用processFile Action

processFile(stream);

这样您就不会离开方法来处理文件。您需要重新构建调用代码,以便处理文件。