...

View Full Version : ASP file upload question



XTGeminiman
08-17-2005, 08:50 PM
Hey guys, I want to change the filename of user submitted files so that I don't get any duplicates. How would I go about doing this?

Can I simply just change the value of the file?



.Fields("fieldname").Value = Fieldname
.Fields("filename").Value = FileName


or is it more complicated?

If you guys need more of the code it's as follow...



<%
const adBinary = 128
const adVarBinary = 204
const adLongVarBinary = 205
const adLongVarchar = 201
const adVarchar = 200
const adBigInt = 20


Dim Upload_FormFields, Upload_FormFiles, Upload_Data, Upload_Outcome
Set Upload_FormFields = Server.CreateObject("Scripting.Dictionary")
Set Upload_FormFiles = Server.CreateObject("Scripting.Dictionary")
Set Upload_Data = Server.CreateObject("ADODB.Recordset")

If Upload_MaxFileSize = "" Then Upload_MaxFileSize = 10485760 ' 10 MB

With Upload_Data
.Fields.Append "fieldname", adVarchar, 255
.Fields.Append "filename", adVarchar, 255
.Fields.Append "size", adBigInt
.Fields.Append "bytes", adBinary , 10485760 ' 10 MB
.Open
End With

Function Upload_ProcessRequest()

dim PosB, PosBBound, PosEBound, PosEHead, PosBFld, PosEFld
dim Boundary, BBoundary, PartBHeader, PartAHeader, PartContent, PartContent2, Binary
dim fso, fle, rst, DataString, FileName
dim I, Length, ContType, PartName, LastPart, BCrlf, PartContentLength

If Request.ServerVariables("REQUEST_METHOD") = "POST" Then

ContType = Request.ServerVariables("HTTP_Content_Type")
If LCase(Left(ContType, 19)) = "multipart/form-data" Then
PosB = InStr(LCase(ContType), "boundary=") 'get boundary
If PosB > 0 Then Boundary = Mid(ContType, PosB + 9) 'we have one

PosB = InStr(LCase(ContType), "boundary=")
If PosB > 0 then
PosB = InStr(Boundary, ",")
If PosB > 0 Then Boundary = Left(Boundary, PosB - 1)
end if

Length = CLng(Request.ServerVariables("HTTP_Content_Length")) 'Get Content-Length header
End If

If Length > 0 And Boundary <> "" Then
Boundary = "--" & Boundary

Binary = Request.BinaryRead(Length)

For I=1 to len(Boundary)
BBoundary = BBoundary & ChrB(Asc(Mid(Boundary,I,1)))
Next

BCrlf = ChrB(Asc(vbCr)) & ChrB(Asc(vbLf))

PosBBound = InStrB(Binary, BBoundary)
PosEBound = InStrB(PosBBound + LenB(BBoundary), Binary, BBoundary, 0)

Do While (PosBBound > 0 And PosEBound > 0)

PosEHead = InStrB(PosBBound + LenB(BBoundary), Binary, BCrlf & BCrlf)

PartBHeader = MidB(Binary, PosBBound + LenB(BBoundary) + 2, PosEHead - PosBBound - LenB(BBoundary) - 2)
PartAHeader = ""
For I=1 to lenb(PartBHeader)
PartAHeader = PartAHeader & Chr(AscB(MidB(PartBHeader,I,1)))
Next

If Right(PartAHeader,1) <> ";" Then PartAHeader = PartAHeader & ";"

PartContent = MidB(Binary, PosEHead + 4, PosEBound - (PosEHead + 4) - 2)

PosBFld = Instr(lcase(PartAHeader),"name=")
If PosBFld > 0 Then
PosEFld = Instr(PosBFld,lcase(PartAHeader),";")
If PosEFld > 0 Then
PartName = Mid(PartAHeader,PosBFld+5,PosEFld-PosBFld-5)
end if
Do Until Left(PartName,1) <> """"
PartName = Mid(PartName,2)
Loop
Do Until Right(PartName,1) <> """"
PartName = Left(PartName,Len(PartName)-1)
Loop
end if

PosBFld = Instr(lcase(PartAHeader),"filename=""")
If PosBFld > 0 Then
PosEFld = Instr(PosBFld + 10,lcase(PartAHeader),"""")
If PosEFld > 0 Then
FileName = Mid(PartAHeader,PosBFld+10,PosEFld-PosBFld-10)
end if
Do Until Left(FileName,1) <> """"
FileName = Mid(FileName,2)
Loop
Do Until Right(FileName,1) <> """"
FileName = Left(FileName,Len(FileName)-1)
Loop
Else
FileName = ""
end if

if vartype(PartContent) = 8 then
' need to do some conversion
Set rst = CreateObject("ADODB.Recordset")
PartContentLength = LenB(PartContent)
if PartContentLength > 0 then
' data, so add to recordset to speed up conversion
rst.Fields.Append "data", adLongVarBinary, PartContentLength
rst.Open
rst.AddNew
rst("data").AppendChunk PartContent & ChrB(0)
rst.Update
PartContent2 = rst("data").GetChunk(PartContentLength)
rst.close
set rst = nothing
else
PartContent2 = ChrB(0)
End If
else
PartContent2 = PartContent
end if

PartContentLength = LenB(PartContent2)
if PartContentLength > 0 then
Set rst = CreateObject("ADODB.Recordset")
rst.Fields.Append "data", adLongVarChar, PartContentLength
rst.Open
rst.AddNew
rst("data").AppendChunk PartContent2
rst.Update
DataString = rst("data")
rst.close
set rst = nothing
Else
dataString = ""
End If


If FileName <> "" Then


FileName = Mid(Filename,InstrRev(FileName,"\")+1)

With Upload_Data
.AddNew
.Fields("fieldname").Value = Fieldname
.Fields("filename").Value = FileName
.Fields("size").Value = Len(datastring)
.Fields("bytes").AppendChunk datastring
.Update
End with

Upload_FormFiles.Add Partname, Filename

else

Upload_FormFields.Add Partname, Datastring

End If

LastPart = MidB(Binary, PosEBound + LenB(BBoundary), 2)

If LastPart = ChrB(Asc("-")) & ChrB(Asc("-")) Then
PosBBound = 0
PosEBound = 0
else
PosBBound = PosEBound
PosEBound = InStrB(PosBBound + LenB(BBoundary), Binary, BBoundary)
End If

loop

Upload_Outcome = "+OK"

else

Upload_Outcome = "-INVALID_REQUEST"

end if

else

Upload_Outcome = "-NOPOST"

end if

end function

Function Upload_SaveFile(FieldName, FileName)

Upload_Data.MoveFirst

Do Until Upload_Data.EOF

If Upload_Data("fieldname").Value = FieldName Then

set fso = Server.CreateObject("Scripting.Filesystemobject")

set fle = fso.CreateTextFile(FileName & "." & t)
DataString = Upload_Data("bytes").GetChunk(Upload_Data("bytes").ActualSize)
fle.write DataString
fle.close

Upload_SaveFile = len(DataString)

set fle = nothing
set fso = nothing

Exit Do

End If
Loop


End Function
%>

Spudhead
08-18-2005, 08:24 AM
It's an unfamiliar upload script but from what I can make out, it'll name the file whatever you set the value of FileName in this function:

Function Upload_SaveFile(FieldName, FileName)

XTGeminiman
08-18-2005, 04:14 PM
ok i requested a few variables and the fieldname varialbes doing nothing. I tried changing the filename variable at that upload part and the scripts just running continuously and then timing out.

Is it possible to alter the script here?



<SCRIPT LANGUAGE="JavaScript">
extArray = new Array(".txt", ".asp");
function LimitAttach(form, file)
{
allowSubmit = false;
if (!file) return;
while (file.indexOf("\\") != -1)
file = file.slice(file.indexOf("\\") + 1);
ext = file.slice(file.indexOf(".")).toLowerCase();
for (var i = 0; i < extArray.length; i++) {
if (extArray[i] == ext) { allowSubmit = true; break; }
}
if (allowSubmit){
form.submit();
}
else
{
alert("Please only upload files that end in types: "
+ (extArray.join(" ")) + "\nPlease select a new "
+ "file to upload and submit again.");
return false;
}
return true;
}
</script>


THat's in the form submission page.

neocool00
08-18-2005, 06:38 PM
If I'm reading this correctly, when you call this function [Upload_SaveFile(FieldName, FileName)], you should be able to pass in any FileName you want, since this is the function that is creating the file.

XTGeminiman
08-18-2005, 07:19 PM
yeah man, that's all i'm trying to pass in right now and it's not working.

I got it like this...

Function Upload_SaveFile(FieldName, crawl)

with crawl="somepage.asp"

and it's just running until the script timeout hits it. maybe there's another place?

i need some sort of function to name the file

XTGeminiman
08-18-2005, 07:31 PM
actually, isn't the file being named right here?



PosBFld = Instr(lcase(PartAHeader),"filename=""")
If PosBFld > 0 Then
PosEFld = Instr(PosBFld + 10,lcase(PartAHeader),"""")
If PosEFld > 0 Then
FileName = Mid(PartAHeader,PosBFld+10,PosEFld-PosBFld-10)
end if
Do Until Left(FileName,1) <> """"
FileName = Mid(FileName,2)
Loop
Do Until Right(FileName,1) <> """"
FileName = Left(FileName,Len(FileName)-1)
Loop
Else
FileName = ""
end if

bilgiy
02-17-2007, 11:50 PM
Hello,
i want to check the files extensions and let the files with .zip,.rar extensions.

how can i modify your code.

Thanks



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum