I am reading a text file containing an insert statement for SQL using C # on the MVC website I'm working on. When debugging the function that I use, it works fine and the insert occurs. But as soon as I publish the site and run it on my local computer (with IIS configured to use asp.net 4.0), it does not seem to work.
if (Request.Files != null && Request.Files["txtImportFile"] != null) { //newFilePath = Server.MapPath("\\" + DateTime.Now.Ticks + Request.Files["txtImportFile"].FileName); string[] temp_string = Request.Files["txtImportFile"].FileName.Split(new char[] { '\\' }); string temp_filename = temp_string[temp_string.Count() - 1]; //newFilePath = Server.MapPath("\\temp\\" + DateTime.Now.Ticks + Request.Files["txtImportFile"].FileName); newFilePath = Server.MapPath("\\temp\\" + DateTime.Now.Ticks + temp_filename); Request.Files["txtImportFile"].SaveAs(newFilePath); StreamReader reader = new StreamReader(newFilePath); string contents = reader.ReadToEnd(); reader.Close(); Models.WingsRemoteDbLibrary dbLib = new Models.WingsRemoteDbLibrary(); string update_message = dbLib.UpdateSlaveItemsTable(contents); if (System.IO.File.Exists(newFilePath)) System.IO.File.Delete(newFilePath); RandomPopupView(update_message); }
I hope my explanations do not seem vague. I will try to answer any other questions. Thank.
Workaround:
Instead of using
Server.MapPath("\\temp\\"...
Create a root folder called "temp" and use
System.Web.HttpContext.Current.Request.MapPath("~\\temp....
, ", " - ! . IIS , . ( ), IIS , . ( IIS - , " " ) ASP.NET, , .
: - -, .. /, /MyApp/? , , , MapPath , .. ~/temp/, ; . , .
/
/MyApp/
MapPath
~/temp/
. , "" . :
Visual Studio "".
" ".
, GetManifestResourceStream:
var stream = GetType() .Assembly .GetManifestResourceStream("YourNameSpace.Folder.YourFile.txt"); using (var reader = new StreamReader(stream)) { var fileContent = reader.ReadToEnd(); }
.