4

I'm reading a text file containing an insert statement for SQL using C# in an MVC Website I'm working on. When debugging the function I'm using works fine and the insert occurs. But once I publish the site and run it on my local machine (with IIS set-up to use asp.net 4.0 even) it doesn't 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 explanation doesn't sound vague. I'll try my best to answer any further questions. Thanks.

tereško
  • 58,060
  • 25
  • 98
  • 150
Erick Garcia
  • 832
  • 2
  • 12
  • 29

3 Answers3

4

Workaround:

Instead of using

Server.MapPath("\\temp\\"...

Create folder under root with name "temp" and use

System.Web.HttpContext.Current.Request.MapPath("~\\temp....
rajesh
  • 285
  • 4
  • 16
  • I'm testing this on my staging website and it works. Now I just need to know what's the difference between Server.MapPath & HttpContext.Current.Request.MapPath – Erick Garcia Apr 18 '11 at 10:11
1

Well, "it doesn't seem to work" is pretty vague - a bit more detail would be nice! But it sounds like a permissions issue. The default profile in IIS has very little access to the disk, especially write access. It isn't really a good idea to write inside your own site anyway (I'd use an unrelated part of the disk, myself), but you will need to configure IIS to run the application in a specific named identity, with access to the disk. Configuring the account itself (not IIS - the account; for example granting "logon as a service") to run as an ASP.NET account is not particularly trivial, unfortunately.

Another thought: is your app a sub-application, i.e. is your app-root /, or is it /MyApp/ ? The reason I ask is your use of MapPath might be better expressed relative to the app-root, i.e. ~/temp/ - but again I stress; writing inside the app is risky. You really want that folder to be non-executing.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Sorry, my explanation is vague. I did think about access as a factor. My code makes a temporary directory to store the file on the server before being read. – Erick Garcia Apr 18 '11 at 06:07
  • Allowing an account to "Logon as service" is not trivial, but it is not hard by any means: http://social.technet.microsoft.com/Forums/en-US/winserverGP/thread/b2aa4009-a392-4817-aecc-5e6c0d982319 – Nate Apr 18 '11 at 06:13
  • @Erick - and it creates successfully? adding another thought, btw – Marc Gravell Apr 18 '11 at 06:13
  • @Nate - yes, it is the other 6 things you need, and remembering them, that get painful ;p – Marc Gravell Apr 18 '11 at 06:14
  • Based on code, it does (it even works when I step-into the code during debug and check the directory if it exist). But after the file is created it is deleted afterwards to avoid unwanted files on the server. – Erick Garcia Apr 18 '11 at 06:52
0

There may be an alternative solution to this problem. You can avoid messing with path and file system altogether if you can 'bake' the file into assembly at build time. Here is how you can do this:

  1. In Visual Studio solution explorer right click on a file and go to Properties.

  2. Set Build Action to 'Embedded Resource'.

  3. Later you can read the file using GetManifestResourceStream:

        var stream = GetType()
            .Assembly
            .GetManifestResourceStream("YourNameSpace.Folder.YourFile.txt");
    
        using (var reader = new StreamReader(stream)) {
            var fileContent = reader.ReadToEnd();
        }
    

    More info here.

Community
  • 1
  • 1
Dmitry
  • 17,078
  • 2
  • 44
  • 70