views:

185

answers:

4

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?

+6  A: 

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.

RC1140
A: 

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

popester
+2  A: 

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.

CMS
A: 

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();
}
gbc