[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

enctype="multipart/form-data" missing form data

Posted on 2008-11-11
7
Medium Priority
?
1,284 Views
Last Modified: 2012-05-05
Hi,

I know when you use enctype="multipart/form-data" you cannot access the data stored Request.Form.

So could you supply me with some code to access this data?

Marc
Form Code:
 
<form action="blogupdate.asp" method="post" enctype="multipart/form-data" name="New Blog" id="New Blog" style="margin-top:0px; margin-bottom:0px">
	<input name="Save_Action" type="hidden" id="Save_Action" value="New_Blog" />
	<input name="Blog_Creator" type="hidden" id="Blog_Creator" value="<%= struser%>" />
	<table width="518" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td style="padding-top:5px; padding-bottom:5px"><span class="style3">Blog Creator: <%= fullname%></span><br />
          <br />
          <input name="Blog_Date" type="text" class="style6" id="Blog_Date" value="<%= Now()%>" readonly="readonly" style="width:120px" /><br />
          <br />
          <input name="Blog_Title" type="text" class="style4" id="Blog_Title" value="Blog Title" style="width:400px" /><br />
          <br />
          <textarea name="Blog_Text" id="Blog_Text" style="color:#999999; font-family:Arial, Helvetica, sans-serif; width:400px; height:150px">Blog Text</textarea><br />
		  <br />
		  <span class="style3">Image</span><br />
          <input type="file" name="Blog_Image" id="Blog_Image" class="style6" style="width:404px" /><br />
          <br />
          Please note all new Blog's are to be vetted before they are viewable.<br />
		  <br />
		  <input type="submit" name="submit" id="submit" value="Save new Blog" /></td>
      </tr>
    </table></form>
 
Action Code:
 
If Request.Form("Save_Action") = "New_Blog" Then
	msql = "SELECT Blog_Main.Blog_ID, Blog_Main.Blog_Creator, Blog_Main.Blog_Title, Blog_Main.Blog_Date, Blog_Main.Blog_Text, Blog_Main.Blog_Image " & _
	"FROM Blog_Main " & _
	"WHERE Blog_ID = 0"
	Set objRS = Server.CreateObject("ADODB.Recordset")
	objRS.Open msql, objConn, adOpenDynamic, adLockOptimistic
	
	objRS.AddNew
	Dim strNewBlog_ID
	strNewBlog_ID = objRS("Blog_ID")
	objRS("Blog_Creator") = Request.Form("Blog_Creator")
	objRS("Blog_Title") = Request.Form("Blog_Title")
	objRS("Blog_Date") = Request.Form("Blog_Date")
	objRS("Blog_Text") = Request.Form("Blog_Text")
	If Request.Form("Blog_Image") <> "" Then
	  objRS("Blog_Image") = Request.Form("Blog_Image")
	Else
	  objRS("Blog_Image") = Null
	End If
	objRS.Update

Open in new window

0
Comment
Question by:marc_butler
  • 4
  • 2
7 Comments
 
LVL 11

Expert Comment

by:Louis01
ID: 22929945
Code is untested, so there might be an error or 2, but the general idea is here.
Assumption made: Views folder exists and is writable (IIS Properties)
<%
Response.Buffer=true
 
'The following three lines of code are used to make sure that this page is not cached on the client.
Response.CacheControl = "no-cache"
Response.AddHeader "Pragma", "no-cache"
Response.Expires = -1
 
Server.ScriptTimeout = 1500
 
Sub BuildUploadRequest(RequestBin)
	'Get the boundary
	PosBeg = 1
	PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(13)))
	boundary = MidB(RequestBin,PosBeg,PosEnd-PosBeg)
	boundaryPos = InstrB(1,RequestBin,boundary)
	'Get all data inside the boundaries
	Do until (boundaryPos=InstrB(RequestBin,boundary & getByteString("--")))
		'Members variable of objects are put in a dictionary object
		Dim UploadControl
		Set UploadControl = CreateObject("Scripting.Dictionary")
		'Get an object name
		Pos = InstrB(BoundaryPos,RequestBin,getByteString("Content-Disposition"))
		Pos = InstrB(Pos,RequestBin,getByteString("name="))
		PosBeg = Pos+6
		PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(34)))
		Name = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
		PosFile = InstrB(BoundaryPos,RequestBin,getByteString("filename="))
		PosBound = InstrB(PosEnd,RequestBin,boundary)
		'Test if object is of file type
		If  PosFile<>0 AND (PosFile<PosBound) Then
			'Get Filename, content-type and content of file
			PosBeg = PosFile + 10
			PosEnd =  InstrB(PosBeg,RequestBin,getByteString(chr(34)))
			FileName = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
			'Add filename to dictionary object
			UploadControl.Add "FileName", FileName
			Pos = InstrB(PosEnd,RequestBin,getByteString("Content-Type:"))
			PosBeg = Pos+14
			PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(13)))
			'Add content-type to dictionary object
			ContentType = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
			UploadControl.Add "ContentType",ContentType
			'Get content of object
			PosBeg = PosEnd+4
			PosEnd = InstrB(PosBeg,RequestBin,boundary)-2
			Value = MidB(RequestBin,PosBeg,PosEnd-PosBeg)
			Else
			'Get content of object
			Pos = InstrB(Pos,RequestBin,getByteString(chr(13)))
			PosBeg = Pos+4
			PosEnd = InstrB(PosBeg,RequestBin,boundary)-2
			Value = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))
		End If
		'Add content to dictionary object
	UploadControl.Add "Value" , Value	
		'Add dictionary object to main dictionary
	UploadRequest.Add name, UploadControl	
		'Loop to next object
		BoundaryPos=InstrB(BoundaryPos+LenB(boundary),RequestBin,boundary)
	Loop
 
End Sub
 
'String to byte string conversion
Function getByteString(StringStr)
 For i = 1 to Len(StringStr)
 	char = Mid(StringStr,i,1)
	getByteString = getByteString & chrB(AscB(char))
 Next
End Function
 
'Byte string to string conversion
Function getString(StringBin)
 getString =""
 For intCount = 1 to LenB(StringBin)
	getString = getString & chr(AscB(MidB(StringBin,intCount,1))) 
 Next
End Function
 
Response.Expires=0
Response.Buffer = TRUE
Response.Clear
 
If Request.TotalBytes > (1000 * 1024) then '(1MB)
	Response.Write "The file size exceeds the allowed size of 1 MB."
	Response.end
end if
 
Response.Clear
byteCount = Request.TotalBytes
 
RequestBin = Request.BinaryRead(byteCount)
Dim UploadRequest
Set UploadRequest = CreateObject("Scripting.Dictionary")
 
BuildUploadRequest  RequestBin
 
contentType = UploadRequest.Item("Blog_Image").Item("ContentType")
filepathname = UploadRequest.Item("Blog_Image").Item("FileName")
filename = Right(filepathname,Len(filepathname)-InstrRev(filepathname,"\"))
value = UploadRequest.Item("Blog_Image").Item("Value")
 
Save_Action = UploadRequest.Item("Save_Action").Item("Value")
Blog_Creator = UploadRequest.Item("Blog_Creator").Item("Value")
Blog_Date = UploadRequest.Item("Blog_Date").Item("Value")
Blog_Title = UploadRequest.Item("Blog_Title").Item("Value")
Blog_Text = UploadRequest.Item("Blog_Text").Item("Value") 'Not sure about using value on textbox - test...
Blog_Image = UploadRequest.Item("Blog_Image").Item("Value")
 
 
Set ScriptObject = Server.CreateObject("Scripting.FileSystemObject")
On Error Resume Next
pathEnd = Len(Server.mappath(Request.ServerVariables("PATH_INFO")))-28 '28 = Length of this asp filename)
If filename <> "" then Set MyFile = ScriptObject.CreateTextFile(Left(Server.mappath(Request.ServerVariables("PATH_INFO")),pathEnd)&"\Views\"&filename) 'Store in Views folder
if err.number <> 0 then
	Response.Write Err.Description & ".<br><br><br>"
	Response.Write "<b>The server does not allow the file to be uploaded to " & Left(Server.mappath(Request.ServerVariables("PATH_INFO")),pathEnd)&"\Views\"&filename & ".<br>"
	Response.End
end if
 
On Error GoTo 0
dim myValue
For i = 1 to LenB(value)
 If filename <> "" then MyFile.Write chr(AscB(MidB(value,i,1)))
Next
	 
If filename <> "" then  MyFile.Close
 
on error goto 0
 
sFilePathName = Server.MapPath("Views/" & filename)
Set MyFile = ScriptObject.GetFile(sFilePathName)
Set MyStream = MyFile.OpenAsTextStream
 
Set connDB = CreateObject("ADODB.Connection")
connDB.Open Application("DBConnectionString")
Set rs = CreateObject("ADODB.Recordset")
sSQLq = "..."
rs.Open CStr(sSQLq), connDB, 1, 2
 
rs.AddNew 
rs("FileName") = filename
rs("Blog_Text") = Replace(Blog_Text, "'", "''") 
rs("ContentType") = ContentType
rs("BinaryDocument").AppendChunk value
rs.Update
rs.Close
		
MyStream.close
MyFile.Delete
%>

Open in new window

0
 
LVL 1

Expert Comment

by:antonio1313
ID: 22933307
Send the other parameter on QueryString. REMEMBER querstring limit (2083 characters). A quick solution. The javascript can be made with a cycle of the elements in the form, but this would work.
<!-- FORM CODE -- >
<script language="javascript">
function sendForm() {
	name1 = document.something.name1.value;
	name2 = document.something.name2.value;
	url = "next_page.asp?name1=" + name1 + "&name2="  + name2;
	document.something.action = url;
	document.something.submit();
}
</script>
<table>
<form name="something" enctype="multipart/form-data">
	<tr>
		<td>Field one:<input type="text" name="name1" value=""></td>
	</tr>
	<tr>
		<td>Field two:<input type="text" name="name2" value=""></td>
	</tr>
	<tr>
		<td>File 1:<input type="file" name="oneFile"></td>
	</tr>
	<tr>
		<td>File 2:<input type="file" name="twoFile"></td>
	</tr>
	<tr>
		<td><input type="button" value="Upload" onClick="sendForm()"></td>
	</tr>
</form>
</table>
 
<!-- ACTION CODE -->
<%
name1 = request.QueryString("name1")
name2 = request.QueryString("name2")
 
sql = "SELECT * FROM table WHERE column=" & name1
...
...
...
THEN WRITE YOU CODE FOR UPLOADING THE FILES
%>

Open in new window

0
 

Author Comment

by:marc_butler
ID: 22937648
Thats not a bad idea, I will do that and let you know the out come.

Marc
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:marc_butler
ID: 22937676
One quick question?
How does the url respond to people putting a return in a multi line textfield?

Marc
0
 

Author Comment

by:marc_butler
ID: 22937830
Hi antonio1313,

Ive tried your Java script but I get an error on page. Attached is my code;
<script language="javascript">
 
function SubmitForm() {
	Blog_Creator = document.New_Blog.Blog_Creator.value;
	Blog_Date = document.New_Blog.Blog_Date.value;
	Blog_Title = document.New_Blog.Blog_Title.value;
	Blog_Text = document.New_Blog.Blog_Text.value;
	url = "blogupdate.asp?Action=Upload&Blog_Creator=" + Blog_Creator + "&Blog_Date="  + Blog_Date + "&Blog_Title="  + Blog_Title + "&Blog_Text="  + Blog_Text;
	document.New_Blog.action = url;
	document.New_Blog.submit();
}
 
</script>
 
<form enctype="multipart/form-data" name="New_Blog" id="New_Blog" style="margin-top:0px; margin-bottom:0px">
	<input name="Blog_Creator" type="hidden" id="Blog_Creator" value="<%= struser%>" />
	<table width="518" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td style="padding-top:5px; padding-bottom:5px"><span class="style3">Blog Creator: <%= fullname%></span><br />
          <br />
          <input name="Blog_Date" type="text" class="style6" id="Blog_Date" value="<%= Now()%>" readonly="readonly" style="width:120px" /><br />
          <br />
          <input name="Blog_Title" type="text" class="style4" id="Blog_Title" value="Blog Title" style="width:400px" /><br />
          <br />
          <textarea name="Blog_Text" id="Blog_Text" style="color:#999999; font-family:Arial, Helvetica, sans-serif; width:400px; height:150px">Blog Text</textarea><br />
		  <br />
		  <span class="style3">Image</span><br />
          <input name="Blog_Image" type="file" class="style6" id="Blog_Image" size="48" />
          <br />
          <br />
          Please note all new Blog's are to be vetted before they are viewable.<br />
		  <br />
		  <input type="button" name="submit" id="submit" value="Save New Blog" onclick="SubmitForm()" /></td>
      </tr>
    </table></form>

Open in new window

0
 

Author Comment

by:marc_butler
ID: 22938664
Hi antonio1313,

I have now sorted the problem and it is all up and running, but I still have one small problem.

The multi line textfield is abit of a pain due to it loosing its formatting on submit. I have got around this problem by getting my user's to use <br /> to end a line. But is there anyway I can keep the formatting on submit?
0
 
LVL 1

Accepted Solution

by:
antonio1313 earned 2000 total points
ID: 22965125
Sorry ... but I had a lot of work
Change the "Save New Blog" button type, submit => button


<!-- ACTION CODE -->
<%
longText = replace(request("Blog_Text"),chr(13),"<br>") 
%>

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question