file upload - How to make a fileupload interface in ASP.NET -


i trying make file upload interface in asp.net webforms , looking advice on how proceed.

the file upload interface part of website making on users can post adverts. interface part of "create new advert" , enable user upload 6 images. using asp.net fileupload server control trying make control work when users have javascript disabled. that's background.

the upload 6 files occurs on button click. stores files in temp folder (/useruploads/temp) until user submits form in case files moved /useruploads folder , references in database or until user hits cancel button or navigates away in case files deleted.

first question is: storing files in temp directory right way go this? or there better way of keeping temp files on server until parent form submitted? alternative can think saving files session, seems recipe killing server...

second question: unclear when user closes browser window. want avoid ending mess of orphaned files in temp directory. there way make sure files cleared out if user doesn't go through form submission? or have perform cleanup of temp directory every often?

third question: doing wrong , there in fact better approach uploading multiple files?

1) if using sql server, prefer store uploaded files in varbinary(max) field , work them unique id. don't have worry name collisions or de-sync of db filesystem. allows upload process independent of insertion of parent form.

the examples below show how grab file stream (and metadata) fileupload control in formview , supply parameter sql stored procedure. then, class implementing ihttphandler used retrieve files db.

2) far clearing out temp files, associate each uploaded file temp master record tied together. when real master confirmed, delete temp master (and reference files real master). run sql agent job on regular interval delete temp masters , associated files older x amount of time.

saving:

protected sub detailsview1_iteminserting(byval sender object, byval e system.web.ui.webcontrols.detailsviewinserteventargs) handles detailsview1.iteminserting     dim objuploader fileupload = detailsview1.findcontrol("fufile")     if objuploader.hasfile         dim strfilename string = objuploader.postedfile.filename         strfilename = strfilename.substring(strfilename.lastindexof("\") + 1)         dim objfilestream system.io.stream = objuploader.postedfile.inputstream         dim arrfileimagebytearray(objfilestream.length) byte         objfilestream.read(arrfileimagebytearray, 0, objfilestream.length)         e.values.insert(0, "fileimage", arrfileimagebytearray)         e.values.insert(1, "filename", strfilename)         e.values.insert(3, "postingdate", now)         e.values.insert(5, "application", "corpforms")     else         e.cancel = true         objmessages.add(new statusmessage(messagetype.warning, "file upload canceled.  no file selected."))     end if end sub 

retrieving:

public class fileservicehandler : implements ihttphandler      public sub processrequest(byval context httpcontext) implements ihttphandler.processrequest          dim idfileid guid         if context.request.querystring("fileid") isnot nothing             dim strfileid string = context.request.querystring("fileid")             try                 idfileid = guid.parse(strfileid)             catch ex exception                 throw new exception("unable parse file id")             end try         end if          dim objconnection new sqlconnection(configurationmanager.connectionstrings("publicwebconnectionstring").connectionstring)         dim objcommand sqlcommand = objconnection.createcommand         dim objreader sqldatareader          objcommand.commandtype = data.commandtype.storedprocedure         objcommand.commandtext = "spgetuploadedfile"         objcommand.parameters.addwithvalue("fileid", idfileid.tostring)          dim arrfileimage() byte = nothing         dim strfilename string = string.empty         try             objconnection.open()             objreader = objcommand.executereader             while objreader.read                 if not isdbnull(objreader("fileimage"))                     arrfileimage = objreader("fileimage")                 end if                 if not isdbnull(objreader("filename"))                     strfilename = objreader("filename")                 end if             end while         catch ex exception             throw new exception("there problem retreiving file: " & ex.message)         end try         if objconnection.state <> data.connectionstate.closed             objconnection.close()         end if         if arrfileimage isnot nothing             context.response.clear()             context.response.addheader("content-disposition", "attachment;filename=" & strfilename)             context.response.binarywrite(arrfileimage)             context.response.end()         else             context.response.contenttype = "text/plain"             context.response.write("unable retrieve file id# " & idfileid.tostring)         end if     end sub      public readonly property isreusable() boolean implements ihttphandler.isreusable                     return true         end     end property  end class 

web.config in file retrieval path:

<configuration>   <system.web>     <httphandlers>       <add verb="get" path="*" type="mynamespace.fileservicehandler" />     </httphandlers>   </system.web>     <system.webserver>         <handlers>             <add name="mynamespace.fileservicehandler" path="*" verb="*" type="mynamespace.fileservicehandler" resourcetype="unspecified" precondition="integratedmode" />         </handlers>     </system.webserver> </configuration> 

Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -