title: Getting Error while downloading file: The process cannot access the file '[filepath]' because it is being used by another process
body: <p>I have an application where I have to upload files and there is an option to download those files. When I upload file it works without any problem but the problem is with download option. When I try to download the upload file it shows below error message</p>
  
  <p><strong>The process cannot access the file '[filepath]' because it is being used by another process.</strong></p>
  
  <p>When I tried after say 30 minutes I didn't get any error but same error occurs if I upload new file and try to download it.</p>
  
  <p>Below is my download and upload code. Please help me to figure out where is the problem.</p>
  
  <p><strong>Upload</strong></p>
  
  <pre><code>FileStream fileStream = File.Create(HttpContext.Current.Server.MapPath("myfile.pdf"), (int)filestream.Length);
  byte[] bytesInStream = new byte[filestream.Length];
  filestream.Read(bytesInStream, 0, bytesInStream.Length);
  fileStream.Write(bytesInStream, 0, bytesInStream.Length);
  </code></pre>
  
  <p><strong>Download</strong></p>
  
  <pre><code>HttpContext.Current.Response.ContentType = "application/pdf";
  HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=myfile.pdf");
  
  FileStream sourceFile = new FileStream(HttpContext.Current.Server.MapPath("myfile.pdf"), FileMode.Open);
  long FileSize;
  FileSize = sourceFile.Length;
  byte[] getContent = new byte[(int)FileSize];
  sourceFile.Read(getContent, 0, (int)sourceFile.Length);
  sourceFile.Close();
  
  HttpContext.Current.Response.BinaryWrite(getContent);
  HttpContext.Current.Response.End();
  </code></pre>
  
  <p>It looks like file was not release after upload but I don't know how to release that file after upload so that it may not create any problem while downloading.</p>