none
How to upload files in Silverlight

    Question

  • Hi,

     I want to upload files into my silverlight application.

    Ex. I want to upload any of the file, let us say .doc or image file into my silverlight Application.

    For this we need to go for a file upload control which exist in asp.net, but not in silverlight.

    How to achieve this using Silverlight.

    Please suggest me i have a need of this.

    Thanks...

     

    Saturday, March 07, 2009 4:45 AM

Answers

  •  Hi anil_litam,

    Let us say my project is in drive c:   Ex. say c:\silverlightuploadapp\...

    I have an abc.doc file in d: drive.

    I want to add this abc.doc file into my project using silverlight programming. In otherwords i want to copy this abc.doc file from location d: drive to c:\silverlightuploadapp\...\abc.doc.

     

    We don't think you can do it as your above description. 

    Based on your said,  you placed your project is in  c:\silverlightuploadapp\..., which is on the server side, But the siverlight runs at client side,

    How can you copy a file to another location  at the client side?

    So we would suggest that you can try to implement this in asp.net instead of using silverlight programming. You can try to refer below sample code:

    string file1 = @"C:/abc";
    string file2 = Server.MapPath("files");

    try
    {
    if(!File.Exists(file2))
    {
    File.Create(file2)
    }

     System.IO.File.Copy(filePath1, filePath2);
    }
    catch(Exception ex)
    {
     Response.Write("Error: " + ex.Message);
    }


    Wednesday, March 11, 2009 4:44 AM

All replies

  • Use OpenFileDialog Box.
    Saturday, March 07, 2009 4:49 AM
  • Thanks for your reply. 

    Any Example code please...

    Saturday, March 07, 2009 4:52 AM
  • I am not trying to open any file rather i want to upload in to my project...
    Saturday, March 07, 2009 5:20 AM
  • You can add files to your project by right-clicking the project and selecting "Add Existing Item."
    Saturday, March 07, 2009 5:30 AM
  • I want to make this through silverlight programming, not manually...
    Saturday, March 07, 2009 5:34 AM
  • So, you'd like to browse the contents of someone's computer, without asking them, rummage through it, and take files off their hard drive? You can't do that in Silverlight -- for some reason.

    Saturday, March 07, 2009 5:38 AM
  • Might you understand differently i think.

    Please go through my requirement clearly once again.

    Let us say my project is in drive c:   Ex. say c:\silverlightuploadapp\...

    I have an abc.doc file in d: drive.

    I want to add this abc.doc file into my project using silverlight programming. In otherwords i want to copy this abc.doc file from location d: drive to c:\silverlightuploadapp\...\abc.doc.

    My requirement is achieving this using file upload control. (In front end i have a fileupload control, where i can click browse then this file has to add to my project folder)

    Waiting for a better reply. Thanks in Advance...

    Saturday, March 07, 2009 5:48 AM
  • I completely understand you. Silverlight does not allow you to browse the hard drive, and it especially does not allow you to create a duplicate of a file. (The exception being if the file is in IsolatedStorage.) If you do not like my answer, wait a while, and someone will come along and confirm it for you.
    Saturday, March 07, 2009 5:53 AM
  •  Hi anil_litam,

    Let us say my project is in drive c:   Ex. say c:\silverlightuploadapp\...

    I have an abc.doc file in d: drive.

    I want to add this abc.doc file into my project using silverlight programming. In otherwords i want to copy this abc.doc file from location d: drive to c:\silverlightuploadapp\...\abc.doc.

     

    We don't think you can do it as your above description. 

    Based on your said,  you placed your project is in  c:\silverlightuploadapp\..., which is on the server side, But the siverlight runs at client side,

    How can you copy a file to another location  at the client side?

    So we would suggest that you can try to implement this in asp.net instead of using silverlight programming. You can try to refer below sample code:

    string file1 = @"C:/abc";
    string file2 = Server.MapPath("files");

    try
    {
    if(!File.Exists(file2))
    {
    File.Create(file2)
    }

     System.IO.File.Copy(filePath1, filePath2);
    }
    catch(Exception ex)
    {
     Response.Write("Error: " + ex.Message);
    }


    Wednesday, March 11, 2009 4:44 AM
  • Because I believe he is trying to build a client app to run on his own computer, not expose it to the public and pull from remote client machine and push it to the server. Is this correct? If so then the only way to do it is to use the open file dialog as noted above. There is no other answer.

    Thursday, March 26, 2009 4:09 PM
  • I have tried this way. Ofcourse pulled out from Some video tutorial

     Added a HTTP Handler to the Web Application

    Code looks like this

     

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Web;

    using System.IO;

    namespace FlyerMaker.Web

    {

    /// <summary>

    /// Summary description for $codebehindclassname$

    /// </summary>

     

    public class ReceiverHandler : IHttpHandler

    {

    public void ProcessRequest(HttpContext context)

    {

    string filename = context.Request.QueryString["filename"].ToString();using (FileStream fs = File.Create(context.Server.MapPath("~/App_Data/" + filename)))

    {

    SaveFile(context.Request.InputStream, fs);

    }

    }

    private void SaveFile(Stream stream, FileStream fs)

    {

    //byte[] buffer = new byte[4096];

    byte[] buffer = new byte[stream.Length];

    int bytesRead;

    while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)

    {

    fs.Write(buffer, 0, bytesRead);

    }

    }

    public bool IsReusable

    {

    get

    {

    return false;

    }

    }

    }

    }

     /***************************************************************************************************************************************************************/

    The following code is in the XAML code behind  - During button click the OpenFileDialog will open , then you can select the file for upload

    void btnUpload_Click(object sender, RoutedEventArgs e)

    {

    // Upload the template files to the server

    OpenFileDialog dlg = new OpenFileDialog();

    dlg.Multiselect = false;

    dlg.Filter = "HTML Files|*.html";if ((bool)dlg.ShowDialog())

    {

    UploadFile(dlg.File.Name, dlg.File.OpenRead());

    }

    else

    {

    //user clicked cancel

    }

    }

     

    private void UploadFile(string fileName,Stream data)

    {

    // Just kept here

    // System.Web.HttpContext.Current.Server

    UriBuilder ub = new UriBuilder("http://localhost:1558/ReceiverHandler.ashx");

    ub.Query = string.Format("fileName={0}", fileName);

    WebClient c = new WebClient();

    c.OpenWriteCompleted += (sender, e) =>

    {

    PushData(data, e.Result);

    e.Result.Close();

    data.Close();

    };

    try

    {

    c.OpenWriteAsync(ub.Uri);

    }

    catch(Exception e)

    {

    MessageBox.Show(e.ToString());

    }

    }

    private void PushData(Stream input, Stream output)

    {

     

    //byte[] buffer = new byte[4096];

    byte[] buffer = new byte[input.Length];

    int bytesRead;

    while ((bytesRead = input.Read(buffer, 0, buffer.Length)) != 0)

    {

    output.Write(buffer, 0, buffer.Length);

    }

    }

     

     

     

     

     

    Tuesday, April 28, 2009 10:40 AM
  • Just read this forum.

    http://www.c-sharpcorner.com/UploadFile/nipuntomar/FileUploadsilverlight03182009030537AM/FileUploadsilverlight.aspx

    Thursday, July 21, 2011 9:01 AM
  • read below

    Wednesday, November 30, 2011 10:06 AM
  • fullsailrick

    wonder why your not working....at least I hope you still are not working because you don't have a clue.  This guy asks a simple question and you come back with your bs, your a joke.  

    Wednesday, November 30, 2011 10:06 AM
  • In Silverlight, for uploading file this link is very good :)

    http://interview-development.blogspot.in/2012/05/silverlight-upload-file.html

    Wednesday, May 02, 2012 3:27 PM