來源:http://hi.baidu.com/raoyao/blog/item/db0e4d4a563d502b09f7efaa.html
今天在項目中使用WebClient從應用程序上傳文件,應該說這是一個很簡單的應用,也就調用一個UploadFile方法而已,然而在實驗時卻遇到了好幾個錯誤,爲此鬱悶了一個上午,現在把我嘗試的經過記錄下來,希望對遇到這類問題的朋友有所幫助!開始我是這樣寫上傳代碼的:
/// 使用WebClient上傳文件測試
/// </summary>
public class WebClientTest
{
public static void Main(string[] args)
{
// Server URL
string uriString = "http://localhost/FileUpLoad/002.gif";
// Local Directory File Info
string fileName = @"c:/temp/002.gif";
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
Console.WriteLine("Uploading {0} to {1} ",fileName,uriString);
// Upload the file to the URL using the HTTP 1.0 POST.
byte[] responseArray = myWebClient.UploadFile(uriString,"POST",fileName);
// Decode and display the response.
Console.WriteLine("/nResponse Received.The contents of the file uploaded are: /n{0}",Encoding.ASCII.GetString(responseArray));
//Waite for User
Console.ReadLine();
}
}
運行後不如人願,彈出了“遠程服務器返回錯誤: (404) 未找到”的錯誤對話框。開始修改方法,把POST修改爲PUT:
/// 使用WebClient上傳文件測試
/// </summary>
public class WebClientTest
{
public static void Main(string[] args)
{
// Server URL
string uriString = "http://localhost/FileUpLoad/002.gif";
// Local Directory File Info
string fileName = @"c:/temp/002.gif";
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
Console.WriteLine("Uploading {0} to {1} ",fileName,uriString);
// Upload the file to the URL using the HTTP 1.0 POST.
byte[] responseArray = myWebClient.UploadFile(uriString,"PUT",fileName);
// Decode and display the response.
Console.WriteLine("/nResponse Received.The contents of the file uploaded are: /n{0}",Encoding.ASCII.GetString(responseArray));
//Waite for User
Console.ReadLine();
}
}
再運行,還是沒有出現想要的提示信息,卻彈出了“遠程服務器返回錯誤: (501) 未實現”的錯誤。沒辦法,Google一把吧,可是找來找去也沒有找到自己想要得解決方法,只到了這樣一段話:
您可以通過如下的方法實現從win application中upload file
假設上傳目錄的物理路徑爲c:/upload,url爲http://localhost/upload
1.在IIS中upload虛擬目錄屬性中的directory security中的anonymous access and authentication control一欄中,點擊edit,選中Anonymous access,並在virtual directory一欄選中write屬性。
2.將c:/upload目錄屬性中的Security設置爲everyone
3.在程序中使用如下的代碼就可以實現file upload
WebClient myclient = new WebClient();
myclient.UploadFile ("http://localhost/upload/odbc.ini","PUT","e://temp//ODBC.INI");
——微軟全球技術中心 技術支持
前面所說的這些權限我都已經設置了啊,而且跟這裏所說的分毫不差,不可能微軟說的也是錯誤的吧。現在我對自己的機器設置開始有點懷疑了。於是讓同事幫我試試,同事機器上竟然上傳成功了!現在問題基本上可以確定出在我的機器上,到底哪兒出問題了呢?
既然錯誤是從服務器上返回的,那就從服務器的IIS開始吧,先允許所有的Web服務擴展。再運行一遍,終於成功了。看來問題就出在了Web服務擴展上了,於是採用排除法,禁止一個測試一遍,這樣終於確定了原來是Web服務擴展中的WebDAV惹得禍。
如果你在使用WebClient上傳文件的過程中遇到了“遠程服務器返回錯誤: (501) 未實現”這樣的錯誤,記得先把Web服務擴展中的WebDAV修改爲允許。現在問題總算解決了,可以鬆口氣了,等等……,問題又來了,我上傳的圖片文件,然而上傳到服務器後卻打不開!再次修改代碼,這次直接以文件流上傳,修改後的代碼如下:
/// 使用WebClient上傳文件測試
/// </summary>
public class WebClientTest
{
public static void Main(string[] args)
{
// Server URL
string uriString = "http://localhost/FileUpLoad/2006327143303_Grid1.jpg";
// Local Directory File Info
string fileName = @"c:/temp/2006327143303_Grid1.jpg";
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
FileStream fs = new FileStream(fileName,FileMode.Open,FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
Byte[] postArray = br.ReadBytes(Convert.ToInt32(fs.Length));
Stream postStream = myWebClient.OpenWrite(uriString,"PUT");
if(postStream.CanWrite)
{
postStream.Write(postArray,0,postArray.Length);
}
postStream.Close();
fs.Close();
}
}
這樣終於可以了,上傳後的圖片也能打開了。可是爲什麼用UploadFile方法上傳後的圖片打不開呢?