Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

I like to open a file. The open option should be shown in a separate window. My code works fine but the open window is not displaying to open a file. Can anyone help me to solve this problem. Here is the code:

private void OpenMyFile()
    {
        string path = GetPath() + ViewState["fileopen"];
        FileInfo file = new FileInfo(ViewState["fileopen"].ToString());
        SqlConnection con = new SqlConnection(@"server=servername;database=DBNAME;uid=ID;pwd=PWD;max pool size=250;Connect Timeout=0");
        con.Open();
        cmd = new SqlCommand("select * from filetypemaster where extension='" + file.Extension + "'", con);
        myReader = cmd.ExecuteReader();
        
        
        if (myReader.Read())
        {
            Response.ContentType =(String)myReader["CONTENT_TYPE"];
            myReader.Close();
        }
        else
        {
            myReader.Close();
            Message("Cannot open selected file");
            return;
        }
       
        Response.WriteFile(path);
        Response.End();
        
        con.Close();
    }
;

Thanks in Advance.
Posted
Updated 13-Jul-11 0:31am
v2
Comments
Sergey Alexandrovich Kryukov 13-Jul-11 14:14pm    
ASP.NET? Tag it!
--Sa

You are missing header.
Response.ContentType = "image/jpeg";
Response.AppendHeader("Content-Disposition","attachment; filename=SailBig.jpg");
Response.TransmitFile( Server.MapPath("~/images/sailbig.jpg") );
Response.End();


Ref:http://www.west-wind.com/weblog/posts/2007/May/21/Downloading-a-File-with-a-Save-As-Dialog-in-ASPNET[^]
 
Share this answer
 
Comments
Member 7946563 13-Jul-11 5:51am    
I like to open a file with all extensions
Prerak Patel 13-Jul-11 5:53am    
You are already assigning content type from database.
You can replace the extension with your file.extension
Response.AppendHeader("Content-Disposition","attachment; filename=Temp." + file.Extension);
Member 7946563 14-Jul-11 2:23am    
Hi,

Its still not working.
Thanks
Maybe you should try something like, looking into the directory, get all the files. Then filter out that list of fileInfos to match your extension. Then you have a List<fileinfo> of all the files with your extension type. Then you can do your open thing.

or

you might want to look into the OpenFileDialog toolbox item in windows Forms
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900