Umar Iqbal
Umar Iqbal

Reputation: 689

Upload a file to asp server

I need to upload text files from my desktop application to webserver that i host i have followed this tutorial ( http://msdn.microsoft.com/en-us/library/36s52zhs.aspx ) however i get this error when i run my code. the remote server returned an error (500) internal server error.

Client side code i.e desktop application.

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));

Server side code i.e. 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>

Upvotes: 1

Views: 1108

Answers (0)

Related Questions