2014-05-18 352 views
-1

我開發了一個ASP.net C#函數來將PDF上傳到數據庫。當我在LocalHost中嘗試它時,它的工作狀態非常好。但是當我在IIS上的服務器上發佈它時。它給了我下面的錯誤,當我點擊上傳:PDF上傳錯誤System.IO.DirectoryNotFoundException:找不到路徑的一部分

System.IO.DirectoryNotFoundException: Could not find a part of the 
path + <path of the file> 
string filePath = Path.GetFullPath(FileUpload1.PostedFile.FileName); 
string filename = Path.GetFileName(filePath); 

FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); 
BinaryReader br = new BinaryReader(fs); 
Byte[] bytes = br.ReadBytes((Int32)fs.Length); 
br.Close(); 
fs.Close(); 

有什麼我應該爲了改變才能夠上傳?

回答

0

當您從本地主機訪問它時,客戶端和服務器都是相同的,因此它可以找到文件。但是當你發佈這兩臺機器時都是孤立的。基本上你沒有從上傳的文件中獲取內容,你所做的是獲取文件名並從本地硬盤獲取數據,你應該使用下面的代碼片段。

int fileLen = fu.PostedFile.ContentLength; 
Byte[] Input = new Byte[fileLen]; 
Stream myStream = fu.PostedFile.InputStream; 
myStream.Read(Input, 0, Input.Length); 

我已經在上傳的文件中聲明瞭一個大小爲字節的字節數組。並從PostedFile InputStream中讀取字節。

相關問題