C# 文件流在最终块C中关闭后不返回#
我使用WCF rest服务以流的形式返回文件,如下所示C# 文件流在最终块C中关闭后不返回#,c#,.net,wcf,rest,C#,.net,Wcf,Rest,我使用WCF rest服务以流的形式返回文件,如下所示 Stream stream = null; var directoryInformation = CommonServices.Utility.DirectoryHelper.GetTempFolderRootLocation(); string newFolderPath = directoryInformation + "\\Attachments\\" + Guid
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);
}
}
添加此代码后,不会在客户端下载此文件。是否有任何方法删除此文件?请帮助我解决此问题您可以在
OperationContext.OperationCompleted
中删除此文件,如中所示
将您的方法更改为此
public void DownloadAttachment(string engagementId, string documentId, Action<Stream> processFile)
这样,您就不会为了处理文件而离开该方法。您需要重新构造调用代码以处理文件。是否在所述方法的末尾添加了此finally块?如果是,,
Close
将处理您尝试返回的相同流。请查看源代码。我已通过添加完整方法更新了我的问题您尝试将文件流返回到已删除的文件?实际上,从服务器读取文件后,我将返回流,以便浏览器将根据内容类型下载此文件。但是在返回流之后,我必须删除该文件。当我尝试在不关闭的情况下删除此文件时,它显示另一进程使用了异常文件。您不能将文件流
返回到要删除的文件,因为文件流
将保留该文件的句柄。为什么不在文件中返回一个字节[]?
public void DownloadAttachment(string engagementId, string documentId, Action<Stream> processFile)
processFile(stream);