2012-06-19 21 views
-1

我上傳文件並將其轉換爲字節並將其保存在數據庫中。它在我的本地系統中正常工作。但我將它託管在專用服務器中,它不能將路徑轉換爲字節。它顯示錯誤使用asp.net在專用服務器上傳文件

'找不到路徑'C:\ fakepath \ 1003.pdf'的一部分。

代碼:

//byte[] bContent = myWebClient.DownloadData(@strFileUploadSubSplit[3]); 
byte[] bContent = null; 

// Open file for reading 
System.IO.FileStream _FileStream = new System.IO.FileStream(strFileUploadSubSplit[3].ToString(), System.IO.FileMode.Open, System.IO.FileAccess.Read); 
// attach filestream to binary reader 
System.IO.BinaryReader _BinaryReader = new System.IO.BinaryReader(_FileStream); 
// get total byte length of the file 
long _TotalBytes = new System.IO.FileInfo(strFileUploadSubSplit[3].ToString()).Length; 
// read entire file into buffer 
bContent = _BinaryReader.ReadBytes((Int32)_TotalBytes); 
// close file reader 
_FileStream.Close(); 
_FileStream.Dispose(); 
_BinaryReader.Close(); 

string base64String = System.Convert.ToBase64String(bContent, 0, bContent.Length); 
str = strFileUploadSubSplit[0] + "*" + strFileUploadSubSplit[1] + "*" + strFileUploadSubSplit[2] + "*" + base64String ; 
string URL = "http://dev2.weicorp.com:81/IApp.svc/IApp/Doc"; 
string ret = string.Empty; 
var webRequest = System.Net.WebRequest.Create(URL) as HttpWebRequest; 
byte[] byteArray = Encoding.UTF8.GetBytes(str); 
webRequest.Method = "POST"; 
webRequest.ContentType = "application/octet-stream"; 
webRequest.ContentLength = byteArray.Length; 
Stream dataStream = webRequest.GetRequestStream(); 
dataStream.Write(byteArray, 0, byteArray.Length); 
dataStream.Close(); 
HttpWebResponse resp = (HttpWebResponse)webRequest.GetResponse(); 
dataStream = resp.GetResponseStream(); 
StreamReader reader = new StreamReader(dataStream); 
ret = reader.ReadToEnd(); 
string status = ret.ToString(); 
context.Response.Write(status); 
+0

您是如何創建路徑的?顯示一些代碼..你有讀訪問文件夾的位置? –

回答

0

你必須只能從上傳的文件中提取的文件名:

string file = Path.GetFileName(uploadedFile.FileName); 

fakepath,你看到的是因爲安全原因Web瀏覽器不發送在客戶端上傳文件的實際路徑。

相關問題