1

我有一个文件,用户浏览并点击上传按钮,我将文件保存在服务器上,appdata/uploads 在应用程序目录中。然后我尝试使用流阅读器读取它,然后解析它。在我的本地开发环境中它工作正常,但是当我将它部署在服务器上时它根本不起作用。有什么建议么??谢谢你

//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;
                }
4

1 回答 1

0

在服务器上保存文件通常会导致权限错误,因为大多数帐户无法写入服务器上的默认位置。您可以使用Path.GetTempFileName,但即使在这种情况下,某些帐户(即请求为“匿名用户”运行的帐户)也无权读取/写入该位置。

如果您只需要解析上传的文件,您可以将流复制到 MemoryStream 并在此内存流上创建 StreamReader。您可能可以直接使用 Stream 上传文件,但它不支持 seaking(可能会在您使用不查找的 StreamReader 时工作)。

于 2012-06-25T02:01:32.837 回答