title: Save and access file on a webserver and localhost
body: <p>I would like to save a file on a webserver from my fully-working fileupload control. I have received the <code>HttpPostedFile file = HttpContext.Current.Request.Files[0];</code> and I would like to save it on my server.</p>
  
  <p>My servers is called "appharbor" ( <a href="https://appharbor.com/" rel="nofollow">https://appharbor.com/</a> ) and it's specification say, that I have file write access for <code>Path.getTempPath();</code> folder.</p>
  
  <p>My code for saving the file is:</p>
  
  <pre><code>HttpPostedFile file = HttpContext.Current.Request.Files[0];
  string dirrectoryPath =Path.GetTempPath();
  string fileName = Path.GetFileName(file.FileName);
  string mapPath = HttpContext.Current.Server.MapPath(dirrectoryPath);
  Directory.CreateDirectory(mapPath);
  string fileFullPath = Path.Combine(mapPath, fileName);
  file.SaveAs(fileFullPath);
  myModel.uri = fileFullPath;
  </code></pre>
  
  <p>How I can remake this code in a working way?</p>
  
  <p>View:</p>
  
  <pre><code>String pdf = "http://docs.google.com/gview?url=" + @Model.uri + "&amp;embedded=true";
  &lt;iframe src="@pdf" style="width:100%; height: 1000px;" frameborder="0"&gt;&lt;/iframe&gt;
  </code></pre>
  
  <h2>As You can see, we are ONLY trying to DISPLAY the file on webpage. Not allowing the user to download it (no special restrictions for it, however).</h2>
  
  <p><strong>EDIT</strong></p>
  
  <p>Detailed information what happens now and with slight change:</p>
  
  <p>When I have </p>
  
  <pre><code>string dirrectoryPath = Path.GetTempPath();
  </code></pre>
  
  <p>then that occurs on server:</p>
  
  <pre><code>POST http://warsztatynet.apphb.com/api/upload 500 (Internal Server Error) 
  </code></pre>
  
  <p>and that occurs locally</p>
  
  <pre><code>An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code
  Additional information: 'C:/Users/Tunczyk/AppData/Local/Temp/' expected virtual path.
  </code></pre>
  
  <p>And I suspect that they're exactly the same thing, becaue both of these happen in same function.</p>
  
  <p><code>Path.GetTempPath();</code> its App_Data folder on webserver and on localhost</p>
  
  <p>When I have</p>
  
  <pre><code>string dirrectoryPath ="~/App_Data";
  </code></pre>
  
  <p>then that occurs on server:</p>
  
  <pre><code>GET http://c/Users/MyPath/sample.rtf 404 (Not Found)
  </code></pre>
  
  <p>and that occurs locally: </p>
  
  <pre><code>GET http://c/Users/MyPath/sample.rtf 404 (Not Found) 
  </code></pre>
  
  <hr>
  
  <p><strong>EDIT 2</strong>
  It's answer for Caótico fanegas post:</p>
  
  <pre><code>Not allowed to load local resource: file:///D:/Users/apphb5dac91d9317868/AppData/Local/Temp/glyphicons-halflings.png 
  </code></pre>
  
  <p>This is what happens when I do it like that:</p>
  
  <pre><code>string dirrectoryPath = Path.GetTempPath();
  string fileName = Path.GetFileName(file.FileName);
  Directory.CreateDirectory(dirrectoryPath);
  string fileFullPath = Path.Combine(dirrectoryPath, fileName);
  file.SaveAs(fileFullPath);
  </code></pre>
  
  <p>Guys I'm looking for easiest way to do this. Its not commercial application. It is just for university.</p>
  
  <hr>
  
  <p><strong>EDIT 3</strong></p>
  
  <p>To be more understandable, I will give You a link to my application:
  <a href="http://warsztatynet.apphb.com/" rel="nofollow">MyWebsite</a>
  To test it You have to:</p>
  
  <ol>
  <li>press Course Creation Tab</li>
  <li>Press button +</li>
  <li>choose any lesson Schema by pressing it</li>
  <li>Press any of coloured divs</li>
  <li>Choose a file type from list</li>
  <li>Upload file</li>
  </ol>
  
  <p>There Should be displayed Your file, but it isn't (in main are of the view - clicked div).</p>
  
  <p>I recomend to use Google Chrome because at other browser You may have some visual problems.</p>