如何使用idhttp上传文件
2016-05-18 10:58
387 查看
标题: 如何使用idhttp上传文件 关键字: 如何使用idhttp上传文件 分类: 个人专区 密级: 公开 (评分: , 回复: 0, 阅读: 22) »» 高分求教如何使用idhttp上传文件,在线等 -------------------------------------------------------------------------------- 代码 procedure TForm1.Button4Click(Sender: TObject); var response:TStringStream; MMPFDS:TMsMultiPartFormDataStream; begin MMPFDS:=TMsMultiPartFormDataStream.Create; response:=TStringStream.Create(''); idhttp1.Request.ContentType:=MMPFDS.RequestContentType; MMPFDS.AddFile('file1','c:\temp\tuneup.txt','text/plain'); MMPFDS.PrepareStreamForDispatch; MMPFDS.Position:=0; try idhttp1.Post('http://192.168.0.14/uploadfile/savetofile.asp',MMPFDS,response); finally showMessage(response.DataString); MMPFDS.Free; response.Free; idhttp1.Disconnect; end; end; --------------------------------------- 错误: HTTP/1.1 500 Internal Server Error. --------------------------------------- 提交页面 <form action='savetofile.asp' method=post enctype="multipart/form-data"> <input type=file name='file1'> <input type=submit> </form> --------------------------------------- 处理页面 <%OPTION EXPLICIT%> <!--#include FILE="upfile_class.asp"--> <% dim upfile,formPath,ServerPath,FSPath,formName,FileName,oFile set upfile=new upfile_class ''建立上传对象 upfile.GetData (10240000) '取得上传数据,限制最大上传10M %> <html> <head> <title>文件上传</title> <style type="text/css"> <!-- .p9{ font-size: 9pt; font-family: 宋体 } --> </style> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> </head> <body leftmargin="20" topmargin="20" class="p9"> <p class="tx1"><font color="#0000FF" size="4"><%=upfile.Version%> </font></p> <hr size=1 noshadow width=300 align=left><% if upfile.err > 0 then '如果出错 select case upfile.err case 1 Response.Write "你没有上传数据呀???是不是搞错了??" case 2 Response.Write "你上传的文件超出我们的限制,最大10M" end select else %> <table border="1" cellpadding="0" cellspacing="0" bordercolor="#000000" class="p9" style="border-collapse: collapse"> <tr bgcolor="#CCCCCC"> <td height="25" valign='middle'>>>本地文件>></td> <td valign='middle'>>>大小(字节)>></td> <td valign='middle'>>>上传到>></td> </tr> <% FSPath=GetFilePath(Server.mappath("upfile.asp"),"\")'取得当前文件在服务器路径 ServerPath=GetFilePath(Request.ServerVariables("HTTP_REFERER"),"/")'取得在网站上的位置 for each formName in upfile.file '列出所有上传了的文件 set oFile=upfile.file(formname) FileName=upfile.form(formName)'取得文本域的值 if not FileName>"" then FileName=oFile.filename'如果没有输入新的文件名,就用原来的文件名 oFile.SaveToFile FSPath&FileName ''保存文件 %> <tr> <td height="20" valign='middle'>>><%=oFile.FilePath&oFile.FileName%>>></td> <td valign='middle'>>><%=oFile.filesize%>>></td> <td valign='middle'>>><A HREF="<%=serverpath&FileName%>"><%=FileName%></A>>></td> </tr><% set oFile=nothing next %> <tr> <td colspan="3" height="25" valign='middle'>>>一共上传了<%=upfile.file.Count%>个文件</td> </tr> <% end if set upfile=nothing '删除此对象 %> </body> </html> <% function GetFilePath(FullPath,str) If FullPath <> "" Then GetFilePath = left(FullPath,InStrRev(FullPath, str)) Else GetFilePath = "" End If End function %> -------------------------------------------------------------------------------- 使用页面的形式上传文件没有问题 -------------------------------------------------------------------------------- up -------------------------------------------------------------------------------- 关注 -------------------------------------------------------------------------------- 关注一下 -------------------------------------------------------------------------------- Const CRLF = #13#10; var s,s1,filename:String; response:TStringStream; source,source1:TMemoryStream; Url:string; i,cnt:integer; begin idhttp.Request.ContentType:='multipart/form-data'; Response := TStringStream.Create(''); url:='Http://'+host+dir; cnt:=files.Count; if (not dead) then begin for i:=0 to cnt-1 do begin filename:=files[i]; if fileexists(filename) then begin try S := '-----------------------------7cf1d6c47c' + CRLF + 'Content-Disposition: form-data; name="file1"; filename="'+filename+'"'+CRLF + 'Content-Type: application/octet-stream' + CRLF + CRLF; //上传文件内容 s1:='file one content. Contant-Type can be application/octet-stream or if'+ 'you want you can ask your OS fot the exact type.' + CRLF + '-----------------------------7cf1d6c47c' + CRLF + //分界符,用于分隔表单(Form)中的各个域 'Content-Disposition: form-data; name="text1"' + CRLF + CRLF + 'hello2' + CRLF + '-----------------------------7cf1d6c47c--'; //提交的下一个表单内容域的内容 s1:=CRLF +'-----------------------------7cf1d6c47c' + CRLF + 'Content-Disposition: form-data; name="text1"' + CRLF + CRLF + 'hello2' + CRLF + '-----------------------------7cf1d6c47c--'; Source := TMemoryStream.Create; Source1 := TMemoryStream.Create; Source1.LoadFromFile(filename); Response:=TStringStream.Create('') ; Response.CopyFrom(source1,source1.Size); s:=s+Response.DataString;//因为只能传字符串 Source.Position :=0; Source.Write(s[1],length(s)); Source.Position :=source.Size ; Source.Write(s1[1],length(s1)); Response.Position :=0; try idHTTP.Post(url, Source, Response); finally if not uploadsuccess(Response.DataString) then begin dead:=true; self.idhttp.Disconnect; end; Source.Free; Response.Free; end;
相关文章推荐
- 深度神经网络权值初始化的诡异问题
- HTTP请求错误大全
- Java 网络编程
- 神经网络之激活函数(Activation Function)
- Delphi IDHTTP用法详解
- Delphi IDHTTP用法详解
- Android 网络编程相关
- HttpClient使用详解
- 深度学习与计算机视觉系列(6)_神经网络结构与神经元激励函数
- 判断网络状态
- tcpdump常用参数说明
- 一点一点学ASP.NET之基础概念——HttpHandler
- 【转】Android网络编程之Socket&Http
- iOS网络4——Reachability检测网络状态
- xmlhttprequest报没有权限
- OKHttp使用简介--使用
- Exception in thread "http-bio-8037-exec-45" java.lang.OutOfMemoryError: PermGen space
- 使用NAT模式对外提供WEB服务的网关的网络过程
- TCP/IP,http,socket,长连接,短连接 总结
- 网络服务调用总结