因为它正由另一个进程使用的FileStream无法访问该文件 [英] FileStream can't access the file because it's being used by another process
问题描述
我在ASP.NET Web应用程序新我碰到这个问题来了。
我有一个页面磨片有下载template.xls是pviously存储在SharePoint页面$ P $的按钮。我有下载方法,它的工作就好了。有问题的模板被保存在那里它应该是,在我的web应用程序的位置,在我的本地IIS文件夹上。
问题是要打开这个文件给最终用户。我需要一个弹出显示给用户,这样他就可以打开/保存该template.xls
我使用的是以下内容:
//在我的本地IIS所在的文件所在的下载后路径
字符串_strFolderApp =〜/ ARQ /;
字符串_strFullFolderApp = _strFolderApp + _strFileName;
字符串apPath = System.Web.Hosting.HostingEnvironment.MapPath(_strFullFolderApp);的FileStream _FileStream =新的FileStream(apPath,FileMode.Create,FileAccess.ReadWrite,FileShare.ReadWrite);
//将字节块使用从字节数组数据此流。
_FileStream.Write(_contentFile,0,_contentFile.Length);//打开文件
Response.Clear();
Response.AddHeader(内容处置,附件;文件名=+ _strFileName);
Response.ContentType =;
Response.TransmitFile(apPath);
Response.Flush();
File.Delete(apPath);
到Response.End();
#endregion//关闭文件流
_FileStream.Close();
我在网上搜索,所有的答案用FileShare.ReadWrite所以这两个过程将正常结束。但它不是为我工作,因为当code到达 Response.TransmitFile(apPath); 我得到一个异常弹出,并没有显示。
例外:
的进程无法访问文件。'C:\\的Inetpub \\ wwwroot的\\ MyWebApp \\文件\\ TemplateSharePoint.xlsx',因为它正被另一个进程使用的
请任何帮助将是AP preciated:)
修改
的 code更新的
如果(_flgSPSDownload)
{
System.IO.FileStream _FileStream =新System.IO.FileStream(apPath,System.IO.FileMode.Create,System.IO.FileAccess.Write);
_FileStream.Write(_contentFile,0,_contentFile.Length);
_FileStream.Close();
Response.Clear();
Response.AddHeader(内容处置,附件;文件名=+ _strFileName);
//设置适当的ContentType。
Response.ContentType =应用程序/ x-msexcel的;
Response.TransmitFile(apPath);
Response.Flush();
//直接将文件写入HTTP内容输出流。
Response.WriteFile(apPath);
//File.Delete(apPath); //Process.Start(_strDestinationPath +//+ _strFileName);
据我所知,你在打开文件流,然后尝试再次打开它,关闭它之前。
该文件的初始开:
的FileStream _FileStream =新的FileStream(apPath,FileMode.Create,FileAccess.ReadWrite,FileShare.ReadWrite);
和
Response.TransmitFile(apPath);
似乎试图再次打开该文件。
我建议叫
_FileStream.Close();
之前调用的TransmitFile
。
I'm new at web app in ASP.NET and I came across this problem.
I have a page whe there is a Button to download a template.xls that is previously stored at a SharePoint page. I have the download method and it's working just fine. The template in question is being saved where it should be, on a folder where my web app is located, on my local IIS. The problem is to open this file to the end user. I need a popup to be displayed to the user, so he can open/save this template.xls I'm using the following :
//path on my local IIS where the file is located after the download
string _strFolderApp = "~/Arq/";
string _strFullFolderApp = _strFolderApp + _strFileName;
string apPath = System.Web.Hosting.HostingEnvironment.MapPath(_strFullFolderApp);
FileStream _FileStream = new FileStream(apPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
// Writes a block of bytes to this stream using data from a byte array.
_FileStream.Write(_contentFile, 0, _contentFile.Length);
//opens the file
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=" + _strFileName);
Response.ContentType = "";
Response.TransmitFile(apPath);
Response.Flush();
File.Delete(apPath);
Response.End();
# endregion
// close file stream
_FileStream.Close();
I searched online and all answers end up in using FileShare.ReadWrite so both process would work properly. But it's not working for me, because when the code reaches Response.TransmitFile(apPath); I get an exception and popup doesn't show. Exception :
The process cannot access the file 'c:\inetpub\wwwroot\MyWebApp\File\TemplateSharePoint.xlsx' because it is being used by another process.
Please any help would be appreciated :)
EDIT Code update
if (_flgSPSDownload)
{
System.IO.FileStream _FileStream = new System.IO.FileStream(apPath,System.IO.FileMode.Create, System.IO.FileAccess.Write);
_FileStream.Write(_contentFile, 0, _contentFile.Length);
_FileStream.Close();
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=" + _strFileName);
//Set the appropriate ContentType.
Response.ContentType = "Application/x-msexcel";
Response.TransmitFile(apPath);
Response.Flush();
//Write the file directly to the HTTP content output stream.
Response.WriteFile(apPath);
//File.Delete(apPath);
//Process.Start(_strDestinationPath + "//" + _strFileName);
From what I can tell, you're opening the file-stream, and then trying to open it again, before you close it.
Initial opening of the file:
FileStream _FileStream = new FileStream(apPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
and
Response.TransmitFile(apPath);
seems to be trying to open the file again.
I would suggest calling
_FileStream.Close();
before calling TransmitFile
.
这篇关于因为它正由另一个进程使用的FileStream无法访问该文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!