如何使用idhttp上传文件

标题: 如何使用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;  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值