1

我需要将文本文件从我的桌面应用程序上传到我托管的网络服务器,我已经按照本教程(http://msdn.microsoft.com/en-us/library/36s52zhs.aspx)但是当我运行我的代码。远程服务器返回错误 (500) 内部服务器错误。

客户端代码,即桌面应用程序。

String uriString = "http://localhost:3045/WebForm1.aspx";

// Create a new WebClient instance.
WebClient myWebClient = new WebClient();

string fileName = @"C:\Users\Administrator\Documents\Visual Studio 2010\Projects\FYP Modules\FileUploader\abc.txt";
MessageBox.Show("Uploading " + fileName + " To " + uriString);

// Upload the file to the URI. 
// The 'UploadFile(uriString,fileName)' method implicitly uses HTTP POST method. 
byte[] responseArray = myWebClient.UploadFile(uriString, fileName);




// Decode and display the response.
MessageBox.Show("\nResponse Received.The contents of the file uploaded are:\n{0}",
                System.Text.Encoding.ASCII.GetString(responseArray));

服务器端代码即asp.net webform

<%@ Import Namespace="System"%>
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System.Net"%>
<%@ Import NameSpace="System.Web"%>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<Script language="C#" runat="server">

void Page_Load(object sender, EventArgs e) {

foreach(string f in Request.Files.AllKeys) {
    HttpPostedFile file = Request.Files[f];
    file.SaveAs("c:\\inetpub\\test\\UploadedFiles\\" + file.FileName);
}   
}

</Script>



<p> Upload complete.  </p>

</asp:Content>
4

0 回答 0