views:

50

answers:

2
+5  A: 

You don't need to use any fancy "web service" technology to upload data from a client application to a server. You can create a simple ASP.NET Web handler and save the input file from the request body:

public class FileUploader : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
       string fileName = context.Request.QueryString["FileName"];
       byte[] fileData = context.Request.BinaryRead(context.Request.ContentLength);
       string path = ...; // decide where you want to save the file
       File.WriteAllBytes(path, fileData);
    }

    public bool IsReusable
    {
        get { return false; }
    }
}

From the client app, you'd use WebClient.UploadFile method:

using (var client = new WebClient()) {
    client.QueryString["FileName"] = fileName;
    client.UploadData(url, inputFileData); 
}

This approach eliminates the overhead of protocols like SOAP and posts the file directly in the HTTP request body.

Mehrdad Afshari
@Mehrdad...thanks for your replay....and +1 for putting one example with that....but i don't know but is this kind direct uploading using http can be useful if user file may be of more then 8-10 mb?
Nitz
@Nitz: "Web service" technologies like SOAP are over HTTP anyway. You can split the file and upload in chunks and have the server merge them back. Otherwise, you'd better use other protocols designed for file transfer.
Mehrdad Afshari
A: 

If you really want to up-/download *files you should maybe take the File Transfer Protocol (FTP).

Oliver