Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
The process cannot access the file 'C:\inetpub\wwwroot\wss\VirtualDirectories\80\Upload\mydoc_2013-05-03.doc' because it is being used by another process.


C#
stream = new FileStream(filename, FileMode.Open, FileAccess.Read,
                                               FileShare.Read);
                       // Total bytes to read:
                       long bytesToRead = stream.Length;
                       Response.ContentType = "application/msword";
                       Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName);
                       // Read the bytes from the stream in small portions.
                       while (bytesToRead > 0)
                       {
                           // Make sure the client is still connected.
                           if (Response.IsClientConnected)
                           {
                               // Read the data into the buffer and write into the
                               // output stream.
                               byte[] buffer = new Byte[10000];
                               int length = stream.Read(buffer, 0, 10000);
                               Response.OutputStream.Write(buffer, 0, length);
                               Response.Flush();
                               // We have already read some bytes.. need to read
                               // only the remaining.
                               bytesToRead = bytesToRead - length;
                           }
                           else
                           {
                               // Get out of the loop, if user is not connected anymore..
                               bytesToRead = -1;
                           }
                       }
                   }
                   catch (Exception ex)
                   {
                       Response.Write(ex.Message);
                       // An error occurred..
                   }
                   finally
                   {
                       if (stream != null)
                       {
                           stream.Close();
                           stream.Dispose();
                       }
                   }
               }


           }
Posted

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900