【问题标题】:FileStream can't access the file because it's being used by another processFileStream 无法访问该文件,因为它正被另一个进程使用
【发布时间】:2012-11-08 20:41:40
【问题描述】:

我是 ASP.NET 网络应用程序的新手,遇到了这个问题。

我有一个页面,其中有一个按钮来下载以前存储在 SharePoint 页面中的 template.xls。我有下载方法,它工作得很好。有问题的模板被保存在我的网络应用程序所在的文件夹中,我的本地 IIS 上。 问题是向最终用户打开这个文件。我需要向用户显示一个弹出窗口,以便他可以打开/保存此 template.xls 我正在使用以下内容:

//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();

我在网上搜索,所有答案最终都使用 FileShare.ReadWrite,因此这两个过程都可以正常工作。但这对我不起作用,因为当代码到达 Response.TransmitFile(apPath); 我得到一个异常并且没有显示弹出窗口。 例外:

进程无法访问文件 'c:\inetpub\wwwroot\MyWebApp\File\TemplateSharePoint.xlsx',因为它正被另一个进程使用。

任何帮助将不胜感激:)

编辑 代码更新

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);

【问题讨论】:

    标签: asp.net filestream savefiledialog response.addheader


    【解决方案1】:

    据我所知,您正在打开文件流,然后尝试再次打开它,然后再关闭它。

    文件的初始打开:

    FileStream _FileStream = new FileStream(apPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
    

    Response.TransmitFile(apPath); 
    

    似乎正在尝试再次打开该文件。

    我建议打电话

    _FileStream.Close();
    

    在致电TransmitFile 之前。

    【讨论】:

    • 感谢您的回答!我试过了,异常消失了。无论如何,下载弹出窗口仍然没有显示,我没有得到任何例外......关于弹出窗口显示可能缺少什么的任何想法?我已经更新了我的问题代码
    • 是的,我需要一个下载对话框来显示,以便用户可以打开/保存文件。在我的研究中,我发现了应该执行此操作的 Response.AddHeader。另外,我在调试时遇到了这个问题:Microsoft JScript 运行时错误:Sys.WebForms.PageRequestManagerParserErrorException:无法解析从服务器收到的消息。
    • @BiancaBorges 我建议您为该例外创建一个新主题。我必须自己研究才能弄清楚它应该如何工作。
    • 我认为例外是因为我在我的页面上使用了 UpdatePanel。快速的 Google 搜索显示下载 x 更新面板可能不兼容。那可能是因为它不起作用......我会继续尝试解决这个问题,如果我再次卡住,我会发布它。再次感谢:)
    • 我知道这是一个老问题,所以不确定您是否仍然遇到文件对话框问题,但上面的代码甚至没有声明它是不好的做法。如果_flgSPSDownload 是那个盒子,那么做if (_flgSPSDownload) 不是你调用它的方式,也不需要流。 msdn.microsoft.com/en-us/library/… 显示如何使用OpenFileDialog 在页面底部选择文件。然后您获取dlg.FileName,并使用Process.Start() 的形式将其打开:msdn.microsoft.com/en-us/library/e8zac0ca(v=vs.110).aspx 在 Excel 中
    【解决方案2】:

    我已经用另一种方式解决了这个问题

    我打开窗口任务管理器 并发现我的项目的dll文件正在运行

    倍数(我的意思是它的副本存在) 我删除它们,然后我的问题就解决了

    【讨论】:

      猜你喜欢
      • 2010-12-10
      相关资源
      最近更新 更多