开发者

"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 + 
               开发者_JS百科 "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?


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.


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.


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜