PDA

View Full Version : Browser Based Uploading of Images


details
10-04-2002, 11:48 PM
The following code is in popup window to a main form.

This code has worked online before for browser based uploading of images.

For some reason when I move it to my local machine running IIS on Windows 2000 professional, I get a page cannot be displayed. It should display the image url to paste into the main form.

This page is called upload.asp

Any ideas or settings I may have missed on my machine?

<%@ Language=VBScript %>

<%
Response.Buffer = true
Function BuildUpload(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
UploadControl.Add "Value" , Value
UploadRequest.Add name, UploadControl
BoundaryPos=InstrB(BoundaryPos+LenB(boundary),RequestBin,boundary)
Loop
End Function

Function getByteString(StringStr)
For i = 1 to Len(StringStr)
char = Mid(StringStr,i,1)
getByteString = getByteString & chrB(AscB(char))
Next
End Function

Function getString(StringBin)
getString =""
For intCount = 1 to LenB(StringBin)
getString = getString & chr(AscB(MidB(StringBin,intCount,1)))
Next
End Function

If request("Action")="1" then
Response.Clear
byteCount = Request.TotalBytes

RequestBin = Request.BinaryRead(byteCount)

Set UploadRequest = CreateObject("Scripting.Dictionary")

BuildUpload(RequestBin)

If UploadRequest.Item("blob").Item("Value") <> "" Then

contentType = UploadRequest.Item("blob").Item("ContentType")
'Response.Write contentType & "<br>"
filepathname = UploadRequest.Item("blob").Item("FileName")
'Response.Write filepathname & "<br>"
filename = Right(filepathname,Len(filepathname)-InstrRev(filepathname,"\"))
Response.Write "<b>" & filename & "</b><br><br>"
FolderName = UploadRequest.Item("where").Item("Value")
'Response.Write FolderName & "<br>"
Path = Mid(Request.ServerVariables("PATH_TRANSLATED"), 1, Len(Request.ServerVariables("PATH_TRANSLATED")) - Len(Request.ServerVariables("PATH_INFO"))) & "\"
'Response.Write Path & "<br>"
ToFolder = Path & "\images\" & FolderName 'change "eshop\" to name of folder where shopping cart application and images folder resides
'Response.Write ToFolder & "<br>"
value = UploadRequest.Item("blob").Item("Value")
'Response.Write value & "<br>"
filename = ToFolder & "\" & filename
'Response.Write filename & "<br>"
Set MyFileObject = Server.CreateObject("Scripting.FileSystemObject")
Set objFile = MyFileObject.CreateTextFile(filename)

For i = 1 to LenB(value)
objFile.Write chr(AscB(MidB(value,i,1)))
Next
objFile.Close
Set objFile = Nothing
Set MyFileObject = Nothing
End If
Set UploadRequest = Nothing
Response.Write "Image successfully uploaded!"
Response.Write "<br>Please copy and paste the above"
Response.Write "<br>bold text into the company logo box"
Response.Write "<br>after the 'images/' text.<br><br>"
Response.Write "<font face='Arial, Verdana, Helvetica, sans-serif' size='2'><b><a href='javascript:window.close();'>Close This Window</a></b></font>"
Response.Write "<br><br><br><br>"
End If

%>

<html>
<head>
<meta name="GENERATOR" Content="Microsoft Visual Studio 6.0">
<script Language="JavaScript">
<!--
function isBlank()
{
if (form1.blob.value == "")
{
alert("You did not select an image to upload.");
form1.blob.focus();
return (false);
}
return (true);
}
// -->
</script>
</head>
<body>
<form method="POST" enctype="multipart/form-data" action="Upload.asp?Action=1" name="form1" id="form1" onsubmit="return isBlank()">

<table cellspacing="0" cellpadding="3" width="100%" border="0">
<tr>
<td colspan="2" align="center"><font face="Arial, Verdana, Helvetica, sans-serif" size="2"><b><a href="javascript:window.close();">Close This Window</a></b><br><br></font></td>
</tr>
<tr>
<td><font face="Arial, Verdana, Helvetica, sans-serif"><b>Select a file to upload:</b></td>
<td><input type="file" name="blob" value> <input type="HIDDEN" name="where" value="Images"></td>
<tr>
<td colspan="2" align="center"><input type="submit" name="Upload" value="Click to Upload"></td>
</tr>
</table>
</form>

</body>
</html>

BigDaddy
10-05-2002, 02:10 AM
What error message is displayed? It should be more specific than the standard "cannot be displayed". Check the advanced settings in your browser options to be sure.

Have you adjusted the read/write permissions on the directory in question? It could be that the server is throwing an error because it does not have the correct permissions.

whammy
10-06-2002, 04:37 AM
P.S. to get the real error (if you're using Internet Explorer), go to Tools>Internet Options and click on the "Advanced" tab.

Then scroll down to where it says "Show friendly HTTP error messages" and make sure it is NOT checked.

Once you do that, refresh the page in question.

:D

Phip
10-06-2002, 06:05 AM
another hint... organize your code :p

details
10-08-2002, 12:07 AM
The code was given to me. The error I am getting with Friendly error turned off is

The page cannot be displayed
There is a problem with the page you are trying to reach and it cannot be displayed.

Roy Sinclair
10-08-2002, 02:07 PM
Originally posted by details
The page cannot be displayed
There is a problem with the page you are trying to reach and it cannot be displayed.

That's the part that's simply telling you there's an error, look further down the page for something in "geekish" for the actual error.

whammy
10-08-2002, 03:52 PM
Can you post it and give us a URL (or the ipaddress/path to the file of the machine hosting it)?