idhttp上传文件到asp失败
upload.htm
<html>
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>文件上传</title>
</head>
<body>
<form method="POST" action="SaveFile.asp">
文件上传:<input type="file" name="file" size="42"> <input type="submit" value="提交" name="bb">
</form>
</body>
</html>
SaveFile.asp
<%
dim file,filename,houzui
file = Request.Form("file")
if file="" then
response.write"<script>alert('请选择要上传的文件!');window.location.href='upload.htm';</script>"
else
houzui=mid(file,InStrRev(file, "."))
if houzui=".jpg" then '允许上传的文件类型
' filename=year(date) & month(date) & day(date) & Hour(time) & minute(time) & second(time) & houzui
filename= "kkk.jpg"
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Type = 1
objStream.Open
objStream.LoadFromFile file
objStream.SaveToFile Server.MapPath(filename),2
objStream.Close
' response.write"<script>alert('图片上传成功!');window.location.href='upload.htm';</script>"
else
' response.write"<script>alert('不允许上传" & houzui & "的格式!');window.location.href='upload.htm';</script>"
end if
end if
procedure TForm1.Button5Click(Sender: TObject);
var
res : String;
ms : TIdMultiPartFormDataStream;
h: TIdhttp;
f:string;
begin
if Opendialog1.Execute then
f:=Opendialog1.FileName;
if f='' then exit;
try
ms := TIdMultiPartFormDataStream.Create;
h := Tidhttp.Create(nil);
ms.AddFile('file',f,'');
idhttp1.Request.ContentType :='multipart/form-data' ;
res:=h.Post('http://127.0.0.1/SaveFile.asp',ms);
if res='上传成功' then
Application.MessageBox('图片上传成功!','提示',MB_OK+MB_ICONASTERISK)
else
Application.MessageBox('图片上传失败!','ERROR',MB_OK+MB_ICONSTOP);
finally
ms.Free;
end;
end;