|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionBrowser-based (HTTP) file uploading is a great way to transfer arbitrary files from a client machine to the Web server which adds another dimension to Web-based applications. However, to enable this feature the client form should be submitted using "multipart/form-data" encoding. Unfortunately, the ASP
For these reasons, I have developed a COM object that users can use instead of
While developing this COM object, I have tried to avoid falling into any of the
previous mentioned problems. However, the solution that I am providing will not
totally replace What is multipart/form-data?This "multipart/form-data" type of encoding is described in RFC1867 which describes how file uploading should be implemented over HTTP protocol. To enable file uploading the form enctype should be set tomultipart/form-data and the method should be set to post.
Then the input tag should be of type file.
For the previous form here is the required code <form method=post action=upload.asp enctype="multipart/form-data"> <input name=name value="Al-Mutairi, Fayez"> <input name=email value="fayezmm@yahoo.com"> <input name=file type=file> <input name=submit type=submit value=Submit> </form> When the user clicks the submit button the web server will receive the following: -----------------------------7d1f5a80420 Content-Disposition: form-data; name="name" Al-Mutairi, Fayez -----------------------------7d1f5a80420 Content-Disposition: form-data; name="email" fayezmm@yahoo.com -----------------------------7d1f5a80420 Content-Disposition: form-data; name="file"; filename="C:\folder_icon.gif" Content-Type: image/gif GIF89a2 2 ³ÿ ÿÿÿÌÌÿ™™ÿffÌUUUDDD33f""" , 2 2 @ éÈI«½8ëÍ»¯H Ž" … ,‹|]HÎ49 7ì!Å Ô³[NGÔ Ž‡CqÉl:‹¯Sê'@žfB©<¯§v †±ž φC$ „+zN¯Ûün$u§/z-Vvc@*<,{bdd UX†ŽlCb=?•#f|jš4fqŒžŸ=>fo¦®¯°±²³N G¬´Ÿ<_z³2ˆT„Á…\R_-ƒ¥µÆ+ƒs”‡ÍŠÃMÑŽ]*gÖ›5ÝAm“áâ—Ökå/ßh Ýìuî[£ò–npÊ…™" ª(úø¨ió/ ®ƒ*¬/ ; -----------------------------7d1f5a80420 Content-Disposition: form-data; name="submit" Submit -----------------------------7d1f5a80420-- At this time when upload.asp started it cannot access the form data using How To Use the ComponentLet us build upload.asp using our <%@ LANGUAGE="JScript%"> <% var form = Server.CreateObject("MyRequest.Form"); form.Init(); Response.Write("Name =" + form("name")); Response.Write("Email =" + form("email")); var file = form.Item("file").Item(1); Response.Write("FileName =" + file.FileName); Response.Write("FileExt =" + file.FileExt); Response.Write("FilePath =" + file.FilePath); Response.Write("MimeType =" + file.ContentType); Response.Write("FileSize =" + file.TotalBytes); // ** To save into a file use Write method of the ADO Stream Object var fileStream = Server.CreateObject("ADODB.Stream"); fileStream.Open(); fileStream.Type = adTypeBinary; fileStream.Write(file.Value); fileStream.SaveToFile(Server.MapPath("Uploaded Files") + "\\" + file.FileName, adSaveCreateOverWrite); fileStream.Close(); // ** To save into a database use AppendChunk method of the Field Object of ADO Recordset var cn = Server.CreateObject("ADODB.Connection"); var rs = Server.CreateObject("ADODB.Recordset"); cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("Database.mdb")); rs.Open("Users", cn, adOpenForwardOnly, adLockOptimistic, adCmdTable); rs.AddNew(); rs.Fields("id") = 1000; rs.Fields("name") = form("name").Value; rs.Fields("email") = form("email").Value; rs.Fields("FileType") = file.ContentType; rs.Fields("file").AppendChunk(file.Value); rs.Update(); rs.Close(); %> Interfaces
Further HelpAdditional help is supplied with the demo project zip file which includes documentation ! Special thanks to Xicoloko who wrote http://www.codeproject.com/asp/uploader.asp which give me a push to also build my own component but this time (Complete).
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||