Anuya
Anuya

Reputation: 8350

"Access denied" when tring to open a file in client side

I a developing a web application in c# in which i am using the below code. Am getting "Access denied" when trying to open a file in client side.

String strPop = "<script language='javascript'>" + Environment.NewLine + 
                "window.open('C://myLocalFile.txt'," +
                "'Report','height=520,width=730," + 
                "toolbars=no,scrollbars=yes,resizable=yes');" + 
                Environment.NewLine + "</script>" + Environment.NewLine;
Page.RegisterStartupScript("Pop", strPop);

What's the problem? and how to overcome it?

Upvotes: 0

Views: 1767

Answers (4)

gbc
gbc

Reputation: 8555

As already stated, you can't use Javascript to open client-side files. However, Silverlight does allow for this, so you could embed a Silverlight control to handle the file as long as you don't mind that dependency.

private void btnOpenFile_Click(object sender, RoutedEventArgs e)
{
    OpenFileDialog dlg = new OpenFileDialog();
    dlg.Filter = "Text Files (.txt)|*.txt|All Files (*.*)|*.*";
    dlg.FilterIndex = 1;
    bool? userClickedOK = dlg.ShowDialog();
    System.IO.Stream fileStream = dlg.File.OpenRead();
    //do whatever you want with the fileStream ...
    fileStream.Close();
}

Upvotes: 0

Christian C. Salvad&#243;
Christian C. Salvad&#243;

Reputation: 827416

JavaScript has strong restrictions about accessing files on the local file system, I think that you are maybe mixing up the client-side and server-side concepts.

JavaScript runs on the client-side, in the client's web browser.

I'm not sure about what you want to achieve, but:

  • If you are trying to open a file on the client machine, you should upload it.

  • If you are trying to open a file on your server, you should put it on an accessible location, within your web application.

Upvotes: 2

popester
popester

Reputation: 1934

Move the file into your website folder and generate a link to it.

Upvotes: 0

RC1140
RC1140

Reputation: 8663

You cant access client side files with JavaScript , the only way to access files is to first upload it to the server or to a flash application.

Upvotes: 6

Related Questions