3

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);
Bianca Borges
  • 175
  • 1
  • 3
  • 8

2 Answers2

1

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.

  • Thanks for the answer! I tried that, and the exception was gone. Anyway, the download popup still doesn't show, and I don't get any exceptions...any ideas of what might be missing for the pop up to show? I have updated my code on my question – Bianca Borges Nov 09 '12 at 17:20
  • Yes, I need a Download dialog box to show so the user can open/save the file. On my research I found out about this Response.AddHeader that is supposed to do this. Also, I'm getting this while debug: Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. – Bianca Borges Nov 09 '12 at 17:33
  • @BiancaBorges i'd suggest you make a new topic for that exception. I'd have to to research to figure out how it's supposed to work myself. – Sam I am says Reinstate Monica Nov 09 '12 at 17:37
  • I think the exception is because I'm using UpdatePanel on my page. A quick Google search showed that there might be a incompatibility with Download x Update Panel. That's probably because it isn't working...I'll continue to try to fix this, if I get stuck again, I'll post it. Thanks again :) – Bianca Borges Nov 09 '12 at 17:41
  • I know this is an old question, so not sure if you're still getting your file dialog issue, but the code above doesn't even declare it & is bad practice. If `_flgSPSDownload` is that box, doing `if (_flgSPSDownload)` isn't how you invoke it, and don't need the stream. https://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog(v=vs.110).aspx shows how to use an `OpenFileDialog` to select a file at the bottom of the page. You then take the `dlg.FileName`, and use a form of `Process.Start()` to open it: https://msdn.microsoft.com/en-us/library/e8zac0ca(v=vs.110).aspx in Excel – vapcguy Nov 23 '16 at 16:40
  • @vapcguy did you intend to post that comment on the question? – Sam I am says Reinstate Monica Nov 23 '16 at 17:38
  • @SamIam No. Bianca was asking about a new error found while trying to implement a download dialog box in this same code. I was assuming she wanted an `OpenFileDialog` and wanted to open the Excel document with it, and allow that to update SharePoint when the user was done with their edits. If it's opened in Excel from this code, however, it looks like it's downloaded to the workstation. It can't then pause while the user does some edits and be re-saved. Another routine would have to periodically sweep the download location and re-upload. – vapcguy Nov 23 '16 at 20:44
0

i have solved this problem by another way

i open Window Task Manager and find that my dll file of my project is running

multiples ( i mean copy of it exists) i remove them and than my problem is solved

Zare Ahmer
  • 808
  • 5
  • 8