vb.net - Exceptional handling in ASP.NET file handler -
i processing pdf/jpg/gif files using http handler.
a popup receives btnid in query string , detects file extension calls handler btnid , processes request.
files physical stored on server file names in db. when there issue file file not found or error want alert displayed , opened window closed not working right now.
i know reason , writing alert response land in either object tag or image tag.
can 1 please suggest how can achieved. appreciated. thanks!!!
public class viewhelpcontent inherits system.web.ui.page 'method dynamically decides based on file extension file viewer render protected sub page_load(byval sender object, byval e system.eventargs) handles me.load dim embed string = string.empty dim count integer dim filename string = string.empty dim extension string = string.empty try if not ispostback if not request.querystring("btnid") nothing dim btnid string = request.querystring("btnid").tostring() count = getfileattachedtobutton(btnid, filename) extension = path.getextension(filename) if extension.tolower() = ".pdf" embed = "<object id=""objpdfviewer"" data=""{0}{1}"" type=""application/pdf"">" embed &= "if unable view file, can download <a href = ""{0}{1}&download=1"">here</a>" embed &= " or there no file available viewed." embed &= "</object>" elseif extension.tolower() = ".jpeg" orelse extension.tolower() = ".jpg" orelse extension.tolower() = ".gif" embed = "<img id=""objimgviewer"" src=""{0}{1}"" alt=""no file available viewed."" />" else clientscript.registerstartupscript(me.gettype(), "alert", "alert('no content uploaded button.');", true) return end if helpcontent.text = string.format(embed, resolveurl("~/processhelpcontent.ashx?btnid="), btnid) end if end if catch ex exception exceptionhandler.logerror(ex) jshelper.showsystemerror(me) end try end sub end class public class processhelpcontent implements system.web.ihttphandler 'sub processes request , generates requested content based on button id sub processrequest(byval context httpcontext) implements ihttphandler.processrequest dim btnid string = string.empty dim filename string = string.empty dim contenttype string = string.empty dim outputdir string = string.empty dim count integer dim strpath fileinfo = nothing dim extension string = string.empty try if not context.request.querystring("btnid") nothing btnid = context.request.querystring("btnid").tostring() outputdir = configurationmanager.appsettings("helpcontentfilesfolder") count = getfileattachedtobutton(btnid, filename) strpath = new fileinfo(outputdir & filename) if not string.isnullorwhitespace(filename) if file.exists(strpath.fullname) extension = path.getextension(strpath.fullname) if context.request.querystring("download") = "1" context.response.appendheader("content-disposition", convert.tostring("attachment; filename=") & filename) end if context.response.cache.setcacheability(httpcacheability.nocache) if extension.tolower() = ".pdf" context.response.contenttype = "application/pdf" elseif extension.tolower() = ".jpg" orelse extension.tolower() = ".jpeg" context.response.contenttype = "image/jpeg" elseif extension.tolower() = ".gif" context.response.contenttype = "image/gif" end if context.response.transmitfile(strpath.fullname) context.response.flush() else context.response.write("<script>alert('uploaded file missing. please re-upload or contact administrator.'); window.close();</script>") end if else context.response.write("<script>alert('uploaded file missing. please re-upload or contact administrator.'); window.close();</script>") end if end if catch ex exception exceptionhandler.logerror(ex) context.response.write("<script>alert('uploaded file missing. please re-upload or contact administrator.'); window.close();</script>") end try end sub readonly property isreusable() boolean implements ihttphandler.isreusable return false end end property end class
Comments
Post a Comment