查看: 1397|回复: 12
|
asp upload file 碰到难题
[复制链接]
|
|
发表于 8-6-2006 07:34 PM
|
显示全部楼层
你有没有看过里面的code先。。。。
我想你因该看看code了才问人。。。 |
|
|
|
|
|
|
|
楼主 |
发表于 8-6-2006 10:31 PM
|
显示全部楼层
原帖由 红发 于 8-6-2006 07:34 PM 发表
你有没有看过里面的code先。。。。
我想你因该看看code了才问人。。。
好!!迟一点 |
|
|
|
|
|
|
|
楼主 |
发表于 9-6-2006 09:58 AM
|
显示全部楼层
这个是freeASPupload.asp 我本身已加入 FS 是为了 在上传的时候判断有没有档案的存在 以免覆盖
只改Public Sub Save1(fname, path)
fname是我自己加的
可是fname是没有value的
- <%
- Class FreeASPUpload
- Public UploadedFiles
- Public FormElements
- Private VarArrayBinRequest
- Private StreamRequest
- Private uploadedYet
- Private FS
- Private Sub Class_Initialize()
- Set UploadedFiles = Server.CreateObject("Scripting.Dictionary")
- Set FormElements = Server.CreateObject("Scripting.Dictionary")
- Set StreamRequest = Server.CreateObject("ADODB.Stream")
- Set FS = Server.CreateObject("Scripting.FileSystemObject")
- StreamRequest.Type = 1 'adTypeBinary
- StreamRequest.Open
- uploadedYet = false
- End Sub
-
- Private Sub Class_Terminate()
- If IsObject(UploadedFiles) Then
- UploadedFiles.RemoveAll()
- Set UploadedFiles = Nothing
- End If
- If IsObject(FormElements) Then
- FormElements.RemoveAll()
- Set FormElements = Nothing
- End If
- if IsObject(FS) Then
-
- Set FS = Nothing
- End If
-
- StreamRequest.Close
- Set StreamRequest = Nothing
- End Sub
- Public Property Get Form(sIndex)
- Form = ""
- If FormElements.Exists(LCase(sIndex)) Then Form = FormElements.Item(LCase(sIndex))
- End Property
- Public Property Get Files()
- Files = UploadedFiles.Items
- End Property
- 'Calls Upload to extract the data from the binary request and then saves the uploaded files
- Public Sub Save1(fname, path)
- Dim streamFile, fileItem
- if Right(path, 1) <> "" then path = path & ""
- if not uploadedYet then Upload1
- For Each fileItem In UploadedFiles.Items
- if not FS.FileExists(path & fileItem.FileName) then
- Set streamFile = Server.CreateObject("ADODB.Stream")
- streamFile.Type = 1
- streamFile.Open
- StreamRequest.Position=fileItem.Start
- StreamRequest.CopyTo streamFile, fileItem.Length
- 'streamFile.SaveToFile path & fileItem.FileName, 2
- 'streamFile.SaveToFile path & "logo1" & right(fileItem.filename,4), 2
- streamFile.SaveToFile Path & fname, 2
- streamFile.close
- Set streamFile = Nothing
- fileItem.Path = path & fileItem.FileName
- 'response.write "<br>" &right(fileItem.filename,4) & "<br>"
- ' response.write "<br>" &fileItem.items&"<br>"
- response.write "<br>" &fname&"<br>"
- response.write "<br>" &path&"<br>"
- else
- response.write "<br><b>file exists</b><br>"
- end if
- Next
-
- End Sub
-
-
- Public Function SaveBinRequest(path) ' For debugging purposes
- StreamRequest.SaveToFile path & "\debugStream.bin", 2
- End Function
- Public Sub DumpData() 'only works if files are plain text
- Dim i, aKeys, f
- response.write "Form Items:<br>"
- aKeys = FormElements.Keys
- For i = 0 To FormElements.Count -1 ' Iterate the array
- response.write aKeys(i) & " = " & FormElements.Item(aKeys(i)) & "<BR>"
- Next
- response.write "Uploaded Files:<br>"
- For Each f In UploadedFiles.Items
- response.write "Name: " & f.FileName & "<br>"
- response.write "Type: " & f.ContentType & "<br>"
- response.write "Start: " & f.Start & "<br>"
- response.write "Size: " & f.Length & "<br>"
- Next
- End Sub
- Private Sub Upload1()
- Dim nCurPos, nDataBoundPos, nLastSepPos
- Dim nPosFile, nPosBound
- Dim sFieldName, osPathSep, auxStr
- 'RFC1867 Tokens
- Dim vDataSep
- Dim tNewLine, tDoubleQuotes, tTerm, tFilename, tName, tContentDisp, tContentType
- tNewLine = Byte2String(Chr(13))
- tDoubleQuotes = Byte2String(Chr(34))
- tTerm = Byte2String("--")
- tFilename = Byte2String("filename=""")
- tName = Byte2String("name=""")
- tContentDisp = Byte2String("Content-Disposition")
- tContentType = Byte2String("Content-Type:")
- uploadedYet = true
- on error resume next
- VarArrayBinRequest = Request.BinaryRead(Request.TotalBytes)
- if Err.Number <> 0 then
- response.write "<br><br><B>System reported this error:</B><p>"
- response.write Err.Description & "<p>"
- response.write "The most likely cause for this error is the incorrect setup of AspMaxRequestEntityAllowed in IIS MetaBase. Please see instructions in the <A HREF='http://www.freeaspupload.net/freeaspupload/requirements.asp'>requirements page of freeaspupload.net</A>.<p>"
- Exit Sub
- end if
- on error goto 0 'reset error handling
- nCurPos = FindToken(tNewLine,1) 'Note: nCurPos is 1-based (and so is InstrB, MidB, etc)
- If nCurPos <= 1 Then Exit Sub
-
- 'vDataSep is a separator like -----------------------------21763138716045
- vDataSep = MidB(VarArrayBinRequest, 1, nCurPos-1)
- 'Start of current separator
- nDataBoundPos = 1
- 'Beginning of last line
- nLastSepPos = FindToken(vDataSep & tTerm, 1)
- Do Until nDataBoundPos = nLastSepPos
-
- nCurPos = SkipToken(tContentDisp, nDataBoundPos)
- nCurPos = SkipToken(tName, nCurPos)
- sFieldName = ExtractField(tDoubleQuotes, nCurPos)
- nPosFile = FindToken(tFilename, nCurPos)
- nPosBound = FindToken(vDataSep, nCurPos)
-
- If nPosFile <> 0 And nPosFile < nPosBound Then
- Dim oUploadFile
- Set oUploadFile = New UploadedFile
-
- nCurPos = SkipToken(tFilename, nCurPos)
- auxStr = ExtractField(tDoubleQuotes, nCurPos)
- ' We are interested only in the name of the file, not the whole path
- ' Path separator is \ in windows, / in UNIX
- ' While IE seems to put the whole pathname in the stream, Mozilla seem to
- ' only put the actual file name, so UNIX paths may be rare. But not impossible.
- osPathSep = ""
- if InStr(auxStr, osPathSep) = 0 then osPathSep = "/"
- oUploadFile.FileName = Right(auxStr, Len(auxStr)-InStrRev(auxStr, osPathSep))
- if (Len(oUploadFile.FileName) > 0) then 'File field not left empty
- nCurPos = SkipToken(tContentType, nCurPos)
-
- auxStr = ExtractField(tNewLine, nCurPos)
- ' NN on UNIX puts things like this in the streaa:
- ' ?? python py type=?? python application/x-python
- oUploadFile.ContentType = Right(auxStr, Len(auxStr)-InStrRev(auxStr, " "))
- nCurPos = FindToken(tNewLine, nCurPos) + 4 'skip empty line
-
- oUploadFile.Start = nCurPos-1
- oUploadFile.Length = FindToken(vDataSep, nCurPos) - 2 - nCurPos
-
- If oUploadFile.Length > 0 Then UploadedFiles.Add LCase(sFieldName), oUploadFile
- End If
- Else
- Dim nEndOfData
- nCurPos = FindToken(tNewLine, nCurPos) + 4 'skip empty line
- nEndOfData = FindToken(vDataSep, nCurPos) - 2
- If Not FormElements.Exists(LCase(sFieldName)) Then
- FormElements.Add LCase(sFieldName), String2Byte(MidB(VarArrayBinRequest, nCurPos, nEndOfData-nCurPos))
- else
- FormElements.Item(LCase(sFieldName))= FormElements.Item(LCase(sFieldName)) & ", " & String2Byte(MidB(VarArrayBinRequest, nCurPos, nEndOfData-nCurPos))
- end if
- End If
- 'Advance to next separator
- nDataBoundPos = FindToken(vDataSep, nCurPos)
- Loop
- StreamRequest.Write(VarArrayBinRequest)
- End Sub
- Private Function SkipToken(sToken, nStart)
- SkipToken = InstrB(nStart, VarArrayBinRequest, sToken)
- If SkipToken = 0 then
- Response.write "Error in parsing uploaded binary request."
- Response.End
- end if
- SkipToken = SkipToken + LenB(sToken)
- End Function
- Private Function FindToken(sToken, nStart)
- FindToken = InstrB(nStart, VarArrayBinRequest, sToken)
- End Function
- Private Function ExtractField(sToken, nStart)
- Dim nEnd
- nEnd = InstrB(nStart, VarArrayBinRequest, sToken)
- If nEnd = 0 then
- Response.write "Error in parsing uploaded binary request."
- Response.End
- end if
- ExtractField = String2Byte(MidB(VarArrayBinRequest, nStart, nEnd-nStart))
- End Function
- 'String to byte string conversion
- Private Function Byte2String(sString)
- Dim i
- For i = 1 to Len(sString)
- Byte2String = Byte2String & ChrB(AscB(Mid(sString,i,1)))
- Next
- End Function
- 'Byte string to string conversion
- Private Function String2Byte(bsString)
- Dim i
- String2Byte =""
- For i = 1 to LenB(bsString)
- String2Byte = String2Byte & Chr(AscB(MidB(bsString,i,1)))
- Next
- End Function
- End Class
- Class UploadedFile
- Public ContentType
- Public Start
- Public Length
- Public Path
- Private nameOfFile
- ' Need to remove characters that are valid in UNIX, but not in Windows
- Public Property Let FileName(fN)
- nameOfFile = fN
- nameOfFile = SubstNoReg(nameOfFile, "", "_")
- nameOfFile = SubstNoReg(nameOfFile, "/", "_")
- nameOfFile = SubstNoReg(nameOfFile, ":", "_")
- nameOfFile = SubstNoReg(nameOfFile, "*", "_")
- nameOfFile = SubstNoReg(nameOfFile, "?", "_")
- nameOfFile = SubstNoReg(nameOfFile, """", "_")
- nameOfFile = SubstNoReg(nameOfFile, "<", "_")
- nameOfFile = SubstNoReg(nameOfFile, ">", "_")
- nameOfFile = SubstNoReg(nameOfFile, "|", "_")
- End Property
- Public Property Get FileName()
- FileName = nameOfFile
- End Property
- 'Public Property Get FileN()ame
- End Class
- %>
复制代码
[ 本帖最后由 quantum^_^ 于 9-6-2006 10:01 AM 编辑 ] |
|
|
|
|
|
|
|
楼主 |
发表于 9-6-2006 10:12 AM
|
显示全部楼层
楼上的fname是在这里传送的
可是不懂为什么没有值
- function SaveFiles
- Dim Upload, fileName, fileSize, ks, i, fileKey, fname
- Set Upload = New FreeASPUpload
- fname = request("enter_a_number")
- Upload.Save1 fname, uploadsDirVar
- ' Upload.Save1 uploadsDirVar
- ' If something fails inside the script, but the exception is handled
- If Err.Number<>0 then Exit function
- SaveFiles = ""
- ks = Upload.UploadedFiles.keys
- if (UBound(ks) <> -1) then
- SaveFiles = ""
- for each fileKey in Upload.UploadedFiles.keys
- SaveFiles = SaveFiles & "<B>Files uploaded:</B> "
- SaveFiles = SaveFiles & Upload.UploadedFiles(fileKey).FileName & " (" & Upload.UploadedFiles(fileKey).Length & "B) "
- SaveFiles = SaveFiles & "<br>Content Type = " & Upload.UploadedFiles(fileKey).ContentType & "<br><br>"
- next
- else
- SaveFiles = "The file name specified in the upload form does not correspond to a valid file in the system."
- end if
- SaveFiles = SaveFiles & "<br>Enter a number = " & Upload.Form("enter_a_number") & "<br>"
- SaveFiles = SaveFiles & "Checkbox values = " & Upload.Form("checkbox_values") & "<br>"
- end function
- %>
复制代码 |
|
|
|
|
|
|
|
发表于 9-6-2006 11:07 AM
|
显示全部楼层
你可以test看用response.write(fname)看看有没有值先。
放在那里我相信你因该知道。^^ |
|
|
|
|
|
|
|
楼主 |
发表于 9-6-2006 11:25 AM
|
显示全部楼层
原帖由 红发 于 9-6-2006 11:07 AM 发表
你可以test看用response.write(fname)看看有没有值先。
放在那里我相信你因该知道。^^
fname 是没有值
Err.Description是这样写
- 006~ASP 0206~Cannot call BinaryRead~Cannot call BinaryRead after using Request.Form collection.
复制代码
我要的是那个filename是动手写不是自动的
动手写是从textbox那里输入
然后用request("textbox")叫出来
可是那个err.description应该是call BinaryRead先才来request.form |
|
|
|
|
|
|
|
发表于 9-6-2006 11:46 AM
|
显示全部楼层
原帖由 quantum^_^ 于 9-6-2006 11:25 AM 发表
fname 是没有值
Err.Description是这样写
006~ASP 0206~Cannot call BinaryRead~Cannot call BinaryRead after using Request.Form collection.
我要的是那个filename是动手写不是自动的
动手写是 ...
这样的话可以给我你的full code吗?
include file name
我test看一下。
因为我not sure是不是要那样写。 |
|
|
|
|
|
|
|
发表于 9-6-2006 11:08 PM
|
显示全部楼层
|
|
|
|
|
|
|
楼主 |
发表于 17-6-2006 01:17 PM
|
显示全部楼层
原帖由 goatstudio 于 9-6-2006 11:08 PM 发表
你的 html form 怎么写?
[ 本帖最后由 quantum^_^ 于 17-6-2006 01:21 PM 编辑 ] |
|
|
|
|
|
|
|
发表于 17-6-2006 02:58 PM
|
显示全部楼层
给你看看我的code先
- <%Dim Form: Set Form = New ASPForm %>
- <!--#INCLUDE FILE="_upload.asp"-->
- <% Form.SizeLimit = &HA00000'10MB
- Const fsCompletted = 0
- IF Form.State = fsCompletted THEN 'Completted
-
- DestinationPath = Server.mapPath("../image/product")
- DestinationFileName = DestinationPath & "/"&Form("SourceFile").FileName
- Form("SourceFile").SaveAs DestinationFileName
- record = 1
- ELSEIF Form.State > 10 THEN
- Const fsSizeLimit = &HD
- Select case Form.State
- case fsSizeLimit: response.write"<br><Font Color=red>Source form size (" & Form.TotalBytes & "B) exceeds form limit (" & Form.SizeLimit & "B)</Font><br>"
- case else response.write "<br><Font Color=red>Some form error.</Font><br>"
- end Select
-
- END IF'Form.State = 0 then
- If record = 1 then
- code=Ucase(Form("code"))
- item=Ucase(Form("item"))
- cate_id=Ucase(Form("cid"))
- prices=Form("prices")
- descri=Ucase(Form("descri"))
- set rs=server.CreateObject("adodb.recordset")
- sql="select * from product where code='"&code&"' and item='"&item&"'"
- rs.open sql,conn,3,2
-
- If not (rs.bof and rs.eof) Then
- rs.close
- response.Redirect("add_new_product.asp?cid="&cate_id&"&action=fail")
- Else
- sql="insert into product(code, item, cate_id, prices, descri, img) values('"&code&"','"&item&"', '"&cate_id&"', '"&prices&"', '"&descri&"' ,'"&Form("SourceFile").FileName&"')"
- conn.Execute(sql)
- Response.Redirect("product_view.asp?cid="&cate_id&"")
- End If
- End if
- %>
复制代码
[ 本帖最后由 红发 于 17-6-2006 03:12 PM 编辑 ] |
|
|
|
|
|
|
|
发表于 17-6-2006 03:06 PM
|
显示全部楼层
上面的code不是最好的。
但是和你的东西比起来是有90%一样的。
还有你所谓的“它会自动create一个folder然后把已选的文件upload在folder里”
那是因为你的code的问题
- savestring = server.mappath("..") + "/LOGO/"
复制代码
上面我说过了。。。。
忘了说。
我的是follow我的file的名
如:我的file name 是 45.gif
save进我的db也是45.gif
来,在比较一下我的code和你的,
希望你可以看出来
[ 本帖最后由 红发 于 17-6-2006 03:15 PM 编辑 ] |
|
|
|
|
|
|
|
发表于 18-6-2006 04:49 AM
|
显示全部楼层
你的script都没有把file upload上,
只是post到file的名字和path罢了。
因为form attribute-> enctype 没有set成 multipart/form-data
所以
line 206:
VarArrayBinRequest = Request.BinaryRead(Request.TotalBytes)
才会有error。 |
|
|
|
|
|
|
| |
本周最热论坛帖子
|