vb.net - ASP.NET Downloading files from an SQL table -
i'm trying download file sql server table. using gridview. whenever try download file, corrupted file.
i using asp.net , vb.net sql server 2012 express.
any ideas why ?
protected sub gridview1_selectedindexchanged(sender object, e eventargs) handles gridview1.selectedindexchanged dim contentid = convert.toint32(gridview1.selectedrow.cells(1).text) dim contentname = gridview1.selectedrow.cells(2).text dim contenttype = gridview1.selectedrow.cells(3).text lblcontentname.text = "[ " + contentname + " ]" lblcontentname.visible = true end sub protected sub gridview1_rowcommand(sender object, e gridviewcommandeventargs) dim cn new sqlconnection("data source=brian-pc\sqlexpress;initial catalog=master_db;integrated security=true;") if e.commandname = "download" dim filename string = string.empty dim id integer = convert.toint32(e.commandargument) dim cmd new sqlcommand("select content_name,content_type,content_data content content_id = " & id, cn) cn.open() dim dr sqldatareader = cmd.executereader dim bytes byte() dr = cmd.executereader() if dr.read() filename = dr("content_name").tostring() response.contenttype = dr("content_type").tostring() response.addheader("content-disposition", "attachment;filename=" & filename) bytes = directcast(dr("content_file"), byte()) response.cache.setcacheability(httpcacheability.nocache) response.binarywrite(bytes) response.flush() response.[end]() end if end if end sub private sub bindgrid() dim constr string = configurationmanager.connectionstrings("connstringdb1").connectionstring using con new sqlconnection(constr) using cmd new sqlcommand() cmd.commandtext = "select content_id, content_name content" cmd.connection = con con.open() gridview1.datasource = cmd.executereader() gridview1.databind() con.close() end using end using end sub protected sub page_load(sender object, e eventargs) handles me.load if not ispostback bindgrid() end if end sub
Comments
Post a Comment