2012-06-25 62 views
1

我有一個文件,用戶瀏覽並點擊上傳按鈕,我將文件保存在服務器上,appdata/uploads在應用程序目錄中。然後我嘗試使用流讀取器讀取它,然後解析它。在我的本地開發環境中,它工作正常,但是當我將其部署在服務器上時,它根本不起作用。有什麼建議麼??謝謝mvc3 c#streamreader文件沒有讀取

//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; 
       } 
+0

我假設該文件被放置在IIS用戶有權訪問的文件夾中?沒有看到太多,這可能是一個安全問題。 –

+2

是否有例外? –

+0

以及即時改變我的方法現在,即時通訊讀取文件保存之前,但做這樣的事情[鏈接] http://msdn.microsoft.com/en-us/library/system.web.httppostedfile.inputstream.aspx 。我只想看看如何將字符串發送到流媒體閱讀器。因爲在我的代碼的其他部分,我使用streamreader來解析我的文件內容。謝謝 – user1475788

回答

0

將文件保存到服務器ususlly會導致權限錯誤,因爲大多數帳戶無法寫入服務器上的默認位置。您可能會忽略使用Path.GetTempFileName,但即使在這種情況下,某些帳戶(即請求針對「匿名用戶」運行的帳戶)也無權讀取/寫入該位置。

如果您只需分析上傳的文件,您可以將流複製到MemoryStream並通過此內存流創建StreamReader。您可以直接使用Stream來上傳文件,但不支持燒錄(可能會像使用StreamReader那樣工作,但不會查找)。

+0

以及流讀取器不讀取服務器上發佈的文件,該文件必須先保存。所以不是保存文件,而是使用httppostedfile.input流來讀取json文件,但結果字符串看起來像字節。有什麼建議麼? – user1475788

+0

不確定爲什麼你不能通過HttpPostedFile.InputStream創建閱讀器。查看[StreamReader(stream,encoding)]的這個構造函數(http://msdn.microsoft.com/en-us/library/ms143456.aspx),它也應該有助於編碼問題。 –

相關問題