views:

33

answers:

1

HI
I Try Upload a file in Silver-Light. for doing this I use System.IO.File to read file bytes and then send the data to service to insert that data as file data in Database.
(Blow Code)

        byte[] data;
        OpenFileDialog open = new OpenFileDialog();
        open.Filter = "Excel Files (*.xlsx)|*.xlsx";
        if (open.ShowDialog()==true)
        {
            open.File.OpenRead();
            data=System.IO.File.ReadAllBytes(open.File.FullName);
            //---- send for service ---Service.savefileindatabase(data);
        }

so when use this code in line which include "ReadAllBytes" exception throw by VS, this is Exception Detail:
"File operation not permitted. Access to path '' is denied."
(for more info that file which i selected was in local Drive)

So What do I do?

+1  A: 

In Silverlight there are far more restrictions than in usual wpf programming. The files you might upload may be only in specific directories and some methods are not available like File.FullName. Only a manually selected file might be uploaded. Your starting code is OK. But in silverlight you do not have access to paths (only to the name).

There are some OpenText, OpenRead methods which allow you to get a stream from the selected file. From that point you'll be able to do what you want with the data coming from the stream.

if (open.ShowDialog())
{
    using (StreamReader reader = open.SelectedFile.OpenRead())
    {
       ...
    }
}
jdehaan
thanks Great Help. also I want to know How write file (Download) in silver-Light.Regards Rev
Rev
It's the same but you open a stream for writing. Maybe you are also interested in Isolated Storage here are answers that might interest you: http://stackoverflow.com/questions/753646/can-i-write-a-file-on-the-clients-pc-using-silverlight
jdehaan