Upload image from Silverlight to ASP .net MVC 2.0 Action
        Posted  
        
            by Raha
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Raha
        
        
        
        Published on 2010-05-15T19:20:58Z
        Indexed on 
            2010/05/15
            19:24 UTC
        
        
        Read the original article
        Hit count: 435
        
Hello ...
I have been trying really hard to Upload a photo from a Silverlight app to ASP .net MVC app.
I simply use WebClient like this in Silverlight :
private bool UploadFile(string fileName, Stream data)
    {
        try
        {
            UriBuilder ub = new UriBuilder("http://localhost:59933/Admin/Upload");
            ub.Query = string.Format("filename={0}", fileName);
            WebClient wc = new WebClient();
            wc.OpenReadCompleted += (sender, e) =>
            {
                PushData(data, e.Result);
                e.Result.Close();
                data.Close();
            };
            wc.OpenWriteAsync(ub.Uri);
        }
        catch (Exception)
        {
            throw;
        }
        return true;
    }
    private void PushData(Stream input, Stream output)
    {
        byte[] buffer = new byte[4096];
        int byteRead;
        while ((byteRead = input.Read(buffer, 0, buffer.Length)) != 0)
        {
            output.Write(buffer, 0, byteRead);
        }
    }
On the other hand I have created a simple action called Upload like this:
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Upload()
    {
         //Some code here
    }
The problem is that it doesn't hit the break point in Upload method.
Is this even possible ? The reason why I am using Silverlight is that I want to preview the image before uploading it to server and it is very simple in Silverlight.
I failed to do that in JavaScript and that is why Silverlight might be more useful here.
© Stack Overflow or respective owner