ASP上传图片和提交表单
有一个添加信息的页面,里面有product产品的名称,产品描述,产品规格,产品类型,产品图片。在添加产品图片这块,怎么做能把图片上传到指定upload目录,而且上传成功后...
有一个添加信息的页面,里面有product 产品的名称,产品描述,产品规格,产品类型,产品图片。在添加产品图片这块,怎么做能把图片上传到指定upload目录,而且上传成功后,文本框里显示的是http路径,旁边显示的是缩略图。 然后点击提交后,将路径提交到 product表中的pic字段中?现在提交其他没问题,但是图片这块没办法搞定, 不知道化境无组件怎么用能实现我要的功能? 我是初级学者
总共需要几个页面来实现呢?每个页面都起什么作用? 展开
总共需要几个页面来实现呢?每个页面都起什么作用? 展开
2个回答
展开全部
表单页面:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript">
function uploadPhoto()
{
sFeatures = "dialogHeight:400px;dialogWidth:400px;";
photovalue=window.showModalDialog("upfile.asp","title",sFeatures);
if(typeof photovalue == "string"){
if(photovalue != ""){
document.getElementById('uploadPhoto').style.display="block";
//document.getElementById('previmage').src='upload/'+photovalue+'';
var oNewNode = document.createElement("LI");
uploadPhotoList.appendChild(oNewNode);
oNewNode.innerHTML='<label><input type="radio" name="uploadphoto" id="uploadphoto" onclick="changeprevimage()" value="'+photovalue+'" /> 使用此主题图片</label><img src="upload/'+photovalue+'" height="50" align="absmiddle" />';
document.getElementById('previmage').src="upload/"+photovalue;
}
}
}
</script>
</head>
<body>
<form action="" name="">
产品名称:<input name="productname" type="text" />
产品图片:<input type="button" value="从电脑中选择图片" onclick="uploadPhoto();" />
<DIV id=uploadPhoto style="DISPLAY: none">
<UL id=uploadPhotoList></UL></DIV>
</form>
</body>
</html>
上传文件页面:upfile.asp
<%
Response.Buffer = True
Server.ScriptTimeOut=9999999
On Error Resume Next
%>
<script language="javascript">
if(window.dialogArguments==null)
{
window.alert("您是非法访问!");
window.history.back(-1);
}
</script>
<%
ExtName = "jpg,gif,png" '允许扩展名
SavePath = "upload" '保存路径
If Right(SavePath,1)<>"/" Then SavePath=SavePath&"/" '在目录后加(/)
CheckAndCreateFolder(SavePath)
UpLoadAll_a = Request.TotalBytes '取得客户端全部内容
If(UpLoadAll_a>0) Then
Set UploadStream_c = Server.CreateObject("ADODB.Stream")
UploadStream_c.Type = 1
UploadStream_c.Open
UploadStream_c.Write Request.BinaryRead(UpLoadAll_a)
UploadStream_c.Position = 0
FormDataAll_d = UploadStream_c.Read
CrLf_e = chrB(13)&chrB(10)
FormStart_f = InStrB(FormDataAll_d,CrLf_e)
FormEnd_g = InStrB(FormStart_f+1,FormDataAll_d,CrLf_e)
Set FormStream_h = Server.Createobject("ADODB.Stream")
FormStream_h.Type = 1
FormStream_h.Open
UploadStream_c.Position = FormStart_f + 1
UploadStream_c.CopyTo FormStream_h,FormEnd_g-FormStart_f-3
FormStream_h.Position = 0
FormStream_h.Type = 2
FormStream_h.CharSet = "GB2312"
FormStreamText_i = FormStream_h.Readtext
FormStream_h.Close
FileName_j = Mid(FormStreamText_i,InstrRev(FormStreamText_i,"\")+1,FormEnd_g)
If(CheckFileExt(FileName_j,ExtName)) Then
SaveFile = Server.MapPath(SavePath & FileName_j)
If Err Then
Response.Write "文件上传: <span style=""color:red;"">文件上传出错!</span> <a href=""" & Request.ServerVariables("URL") &""">重新上传文件</a><br />"
Err.Clear
Else
SaveFile = CheckFileExists(SaveFile)
k=Instrb(FormDataAll_d,CrLf_e&CrLf_e)+4
l=Instrb(k+1,FormDataAll_d,leftB(FormDataAll_d,FormStart_f-1))-k-2
FormStream_h.Type=1
FormStream_h.Open
UploadStream_c.Position=k-1
UploadStream_c.CopyTo FormStream_h,l
FormStream_h.SaveToFile SaveFile,2
SaveFileName = Mid(SaveFile,InstrRev(SaveFile,"\")+1)
session("savefilename")=SaveFileName
%>
<script language="javascript" type="text/javascript">
alert("照片上传成功");
parent.document.getElementById("insertDiv").style.display="block";
parent.document.getElementById("photoPath").value="<%=SaveFileName%>";
parent.document.getElementById('photoPathView').src="upload/"+"<%=SaveFileName%>";
</script>
<%
End If
Else
Response.write "文件上传: <span style=""color:red;"">文件格式不正确!</span> <a href=""" & Request.ServerVariables("URL") &""">重新上传文件</a><br />"
End If
Else
%>
<script language="Javascript">
<!--
function uploadfile()
{
if(document.getElementById('src').value=='')
{
alert('请选择一个图片文件(gif/jpeg/jpg)!!');
return false;
}
}
function finish()
{
returnValue=document.getElementById("photoPath").value;
close();
}
function c()
{
close();
}
// -->
</script>
<form action='<%= Request.ServerVariables("URL") %>' method='post' name="upform" target="uploadframe" enctype="multipart/form-data"> <IFRAME style="DISPLAY: none" name=uploadframe src="about:blank"></IFRAME>
<font style="font-size:12px;">文件上传:</font>
<input name='src' type='file' id="src" style="height:18px;border:1px solid #CCCCCC" size="40" >
<br />
<font style="font-size:12px;">验证码:</font> <input name="checkcode" id="checkcode" maxlength="20" size="10" class="tableBorder" ><img src="inc/GetCode.asp" />
<br />
<input type='submit' value="上传照片" onClick="return uploadfile();" />
<INPUT onclick="c();" type="button" value="放弃上传并关闭窗口">
<INPUT id=photoPath type=hidden name=photoPath>
<div id="insertDiv" style="DISPLAY: none"><IMG id=photoPathView height=50></div>
<LABEL><INPUT onclick=finish(); type=button value=完成上传并关闭窗口> </LABEL>
<p style="font-size:12px;">您上传的照片和祝福可直接印到下面的个性实物礼品上。所以请认真选择照片。像素最好是:1024*768</p>
</form>
<%
End if
Set FormStream_h = Nothing
UploadStream.Close
Set UploadStream = Nothing
%>
<%
'判断文件类型是否合格
Function CheckFileExt(FileName,ExtName) '文件名,允许上传文件类型
FileType = ExtName
FileType = Split(FileType,",")
For i = 0 To Ubound(FileType)
If LCase(Right(FileName,3)) = LCase(FileType(i)) then
CheckFileExt = True
Exit Function
Else
CheckFileExt = False
End if
Next
End Function
'检查上传文件夹是否存在,不存在则创建文件夹
Function CheckAndCreateFolder(FolderName)
fldr = Server.Mappath(FolderName)
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(fldr) Then
fso.CreateFolder(fldr)
End If
Set fso = Nothing
End Function
'检查文件是否存在,重命名存在文件
Function CheckFileExists(FileName)
Set fso=Server.CreateObject("Scripting.FileSystemObject")
If fso.FileExists(SaveFile) Then
i=1
msg=True
Do While msg
CheckFileExists = Replace(SaveFile,Right(SaveFile,4),"_" & i & Right(SaveFile,4))
If not fso.FileExists(CheckFileExists) Then
msg=False
End If
i=i+1
Loop
Else
CheckFileExists = FileName
End If
Set fso=Nothing
End Function
%>
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript">
function uploadPhoto()
{
sFeatures = "dialogHeight:400px;dialogWidth:400px;";
photovalue=window.showModalDialog("upfile.asp","title",sFeatures);
if(typeof photovalue == "string"){
if(photovalue != ""){
document.getElementById('uploadPhoto').style.display="block";
//document.getElementById('previmage').src='upload/'+photovalue+'';
var oNewNode = document.createElement("LI");
uploadPhotoList.appendChild(oNewNode);
oNewNode.innerHTML='<label><input type="radio" name="uploadphoto" id="uploadphoto" onclick="changeprevimage()" value="'+photovalue+'" /> 使用此主题图片</label><img src="upload/'+photovalue+'" height="50" align="absmiddle" />';
document.getElementById('previmage').src="upload/"+photovalue;
}
}
}
</script>
</head>
<body>
<form action="" name="">
产品名称:<input name="productname" type="text" />
产品图片:<input type="button" value="从电脑中选择图片" onclick="uploadPhoto();" />
<DIV id=uploadPhoto style="DISPLAY: none">
<UL id=uploadPhotoList></UL></DIV>
</form>
</body>
</html>
上传文件页面:upfile.asp
<%
Response.Buffer = True
Server.ScriptTimeOut=9999999
On Error Resume Next
%>
<script language="javascript">
if(window.dialogArguments==null)
{
window.alert("您是非法访问!");
window.history.back(-1);
}
</script>
<%
ExtName = "jpg,gif,png" '允许扩展名
SavePath = "upload" '保存路径
If Right(SavePath,1)<>"/" Then SavePath=SavePath&"/" '在目录后加(/)
CheckAndCreateFolder(SavePath)
UpLoadAll_a = Request.TotalBytes '取得客户端全部内容
If(UpLoadAll_a>0) Then
Set UploadStream_c = Server.CreateObject("ADODB.Stream")
UploadStream_c.Type = 1
UploadStream_c.Open
UploadStream_c.Write Request.BinaryRead(UpLoadAll_a)
UploadStream_c.Position = 0
FormDataAll_d = UploadStream_c.Read
CrLf_e = chrB(13)&chrB(10)
FormStart_f = InStrB(FormDataAll_d,CrLf_e)
FormEnd_g = InStrB(FormStart_f+1,FormDataAll_d,CrLf_e)
Set FormStream_h = Server.Createobject("ADODB.Stream")
FormStream_h.Type = 1
FormStream_h.Open
UploadStream_c.Position = FormStart_f + 1
UploadStream_c.CopyTo FormStream_h,FormEnd_g-FormStart_f-3
FormStream_h.Position = 0
FormStream_h.Type = 2
FormStream_h.CharSet = "GB2312"
FormStreamText_i = FormStream_h.Readtext
FormStream_h.Close
FileName_j = Mid(FormStreamText_i,InstrRev(FormStreamText_i,"\")+1,FormEnd_g)
If(CheckFileExt(FileName_j,ExtName)) Then
SaveFile = Server.MapPath(SavePath & FileName_j)
If Err Then
Response.Write "文件上传: <span style=""color:red;"">文件上传出错!</span> <a href=""" & Request.ServerVariables("URL") &""">重新上传文件</a><br />"
Err.Clear
Else
SaveFile = CheckFileExists(SaveFile)
k=Instrb(FormDataAll_d,CrLf_e&CrLf_e)+4
l=Instrb(k+1,FormDataAll_d,leftB(FormDataAll_d,FormStart_f-1))-k-2
FormStream_h.Type=1
FormStream_h.Open
UploadStream_c.Position=k-1
UploadStream_c.CopyTo FormStream_h,l
FormStream_h.SaveToFile SaveFile,2
SaveFileName = Mid(SaveFile,InstrRev(SaveFile,"\")+1)
session("savefilename")=SaveFileName
%>
<script language="javascript" type="text/javascript">
alert("照片上传成功");
parent.document.getElementById("insertDiv").style.display="block";
parent.document.getElementById("photoPath").value="<%=SaveFileName%>";
parent.document.getElementById('photoPathView').src="upload/"+"<%=SaveFileName%>";
</script>
<%
End If
Else
Response.write "文件上传: <span style=""color:red;"">文件格式不正确!</span> <a href=""" & Request.ServerVariables("URL") &""">重新上传文件</a><br />"
End If
Else
%>
<script language="Javascript">
<!--
function uploadfile()
{
if(document.getElementById('src').value=='')
{
alert('请选择一个图片文件(gif/jpeg/jpg)!!');
return false;
}
}
function finish()
{
returnValue=document.getElementById("photoPath").value;
close();
}
function c()
{
close();
}
// -->
</script>
<form action='<%= Request.ServerVariables("URL") %>' method='post' name="upform" target="uploadframe" enctype="multipart/form-data"> <IFRAME style="DISPLAY: none" name=uploadframe src="about:blank"></IFRAME>
<font style="font-size:12px;">文件上传:</font>
<input name='src' type='file' id="src" style="height:18px;border:1px solid #CCCCCC" size="40" >
<br />
<font style="font-size:12px;">验证码:</font> <input name="checkcode" id="checkcode" maxlength="20" size="10" class="tableBorder" ><img src="inc/GetCode.asp" />
<br />
<input type='submit' value="上传照片" onClick="return uploadfile();" />
<INPUT onclick="c();" type="button" value="放弃上传并关闭窗口">
<INPUT id=photoPath type=hidden name=photoPath>
<div id="insertDiv" style="DISPLAY: none"><IMG id=photoPathView height=50></div>
<LABEL><INPUT onclick=finish(); type=button value=完成上传并关闭窗口> </LABEL>
<p style="font-size:12px;">您上传的照片和祝福可直接印到下面的个性实物礼品上。所以请认真选择照片。像素最好是:1024*768</p>
</form>
<%
End if
Set FormStream_h = Nothing
UploadStream.Close
Set UploadStream = Nothing
%>
<%
'判断文件类型是否合格
Function CheckFileExt(FileName,ExtName) '文件名,允许上传文件类型
FileType = ExtName
FileType = Split(FileType,",")
For i = 0 To Ubound(FileType)
If LCase(Right(FileName,3)) = LCase(FileType(i)) then
CheckFileExt = True
Exit Function
Else
CheckFileExt = False
End if
Next
End Function
'检查上传文件夹是否存在,不存在则创建文件夹
Function CheckAndCreateFolder(FolderName)
fldr = Server.Mappath(FolderName)
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(fldr) Then
fso.CreateFolder(fldr)
End If
Set fso = Nothing
End Function
'检查文件是否存在,重命名存在文件
Function CheckFileExists(FileName)
Set fso=Server.CreateObject("Scripting.FileSystemObject")
If fso.FileExists(SaveFile) Then
i=1
msg=True
Do While msg
CheckFileExists = Replace(SaveFile,Right(SaveFile,4),"_" & i & Right(SaveFile,4))
If not fso.FileExists(CheckFileExists) Then
msg=False
End If
i=i+1
Loop
Else
CheckFileExists = FileName
End If
Set fso=Nothing
End Function
%>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询