简体   繁体   English

MVC3 C#StreamReader文件无法读取

[英]mvc3 c# streamreader file not reading

I have a file, which the users browse and hit the upload button, i save the file on the server, appdata/uploads in the application directory. 我有一个文件,用户可以浏览并单击“上传”按钮,将文件保存在服务器上,将appdata / uploads保存在应用程序目录中。 then i try to read it using stream reader and then parse it. 然后我尝试使用流读取器读取它,然后解析它。 on my local development enviornment it works fine, but when i deploy it on a server it does not work at all. 在我的本地开发环境中,它工作正常,但是当我将其部署在服务器上时,它根本无法工作。 any suggestions?? 有什么建议么?? thank you 谢谢

//Save LoadList File:
            DateTime uploadDate = DateTime.Now;
            string destinationPath = string.Format("{0}\\{1}\\{2}\\{3}\\", Server.MapPath("~/App_Data/uploads"), uploadDate.ToString("yyyy"), uploadDate.ToString("MMM"), uploadDate.ToString("dd"));
              if (!Directory.Exists(destinationPath))
                    Directory.CreateDirectory(destinationPath);

              string storedFileName = string.Format("{0}{1}.json", destinationPath, System.Guid.NewGuid());
                file.ElementAt(0).SaveAs(storedFileName);

           //FileImport is a static class
          var Pair = FileImport.CyclesCompleted(storedFileName);





           private static string LoadTextFromFile(string fileName)
                {
                    StreamReader streamReader = new StreamReader(fileName);
                    string text = streamReader.ReadToEnd();
                    streamReader.Close();
                    return text;
                }

Saving file on server ususlly results in permission errors since most accounts can't write to default location on server. 由于大多数帐户无法写入服务器上的默认位置,因此将文件保存在服务器上通常会导致权限错误。 You may get away with using Path.GetTempFileName , but even in this case some accounts (ie account that requests run for "anonymous user") will not have permissions to read/write to that location. 您可能无法使用Path.GetTempFileName ,但是即使在这种情况下,某些帐户(即,为“匿名用户”运行请求的帐户)也没有读取/写入该位置的权限。

If you simply need to parse uploaded file you can copy stream to MemoryStream and create StreamReader over this memory stream. 如果您只需要解析上传的文件,则可以将流复制到MemoryStream并在此内存流上创建StreamReader。 You may be able to use Stream for uploaded file directly, but it will not support seaking (may work as you are using StreamReader which does not seek). 您也许可以直接使用Stream来上传文件,但是它不支持Seaking(可能会因为您正在使用不查找的StreamReader而起作用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM