html页面上 如何上传文件

wawalike 2010-01-06 11:39:58
想在HTML页面上,做上传图片的操作,一直搞不定,希望有人能帮帮忙!
...全文
33241 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
tingtingtuo123 2011-11-09
  • 打赏
  • 举报
回复
这个帖子还不错
论文MVP网 2010-05-28
  • 打赏
  • 举报
回复
kao靠 服了,看清楚是HTML 晕


HTML里面加个IFRAME引用一个ASPX页,然后在ASPX页上面写
s407798637 2010-05-28
  • 打赏
  • 举报
回复
!!!nduo
xray2005 2010-01-07
  • 打赏
  • 举报
回复

<table>
<tr>
<td>
<asp:Panel runat="server" ID="ImageUpPanel" Height="30px">
信息图片  <asp:FileUpload ID="imageFile" runat="server" Width="400" /></asp:Panel>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnOk" runat="server" Text="确 定" Width="80px" OnClick="btnOk_Click"
Height="30px" />
</td>
</tr>
</table>


后台代码:

protected void btnOk_Click(object sender, EventArgs e)
{
string imgId = Guid.NewGuid().ToString();
string extString = string.Empty;
string imagUrl = string.Empty;
if (!string.IsNullOrEmpty(this.imageFile.FileName.Trim()))
{
int posInt = this.imageFile.FileName.LastIndexOf('.');
if (posInt > -1)
{
extString = postFileName.Substring(posInt + 1);//扩展名
}
imagUrl = @"~/Images/InfoImages/" + imgId + "." + extString;
this.imageFile.SaveAs(Server.MapPath(imagUrl));

}
}
追寻风的自由 2010-01-07
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 mngzilin 的回复:]
HTML code<body><formid="form1" runat="server"><div><inputid="File1" type="file" name="sz" runat="server" style="width:0px; height:0px; border:0;"/><asp:ButtonID="Button1" runat="server" OnClick="Button1_Click" Text="Button" Width=0/></div></form></body>

C# codeprotectedvoid Button1_Click(object sender, EventArgs e)
{string ss= File1.Value;//下断点可以看到已经获取到客户端路径 files[0].SaveAs("C:\\2.txt");//保存到服务端路径C:\\下}
[/Quote]
+1
mngzilin 2010-01-07
  • 打赏
  • 举报
回复
<body>
<form id="form1" runat="server">
<div>
<input id="File1" type="file" name="sz" runat="server" style="width:0px; height:0px; border:0;"/>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" Width=0 /></div>
</form>
</body>


protected void Button1_Click(object sender, EventArgs e)
{
string ss = File1.Value;//下断点可以看到已经获取到客户端路径
files[0].SaveAs("C:\\2.txt");//保存到服务端路径C:\\下
}
月之点点 2010-01-07
  • 打赏
  • 举报
回复
HTML上放个falsh
-无-为- 2010-01-07
  • 打赏
  • 举报
回复
zhi支持周公 给楼主顶
cfesc9 2010-01-07
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 talesten 的回复:]
顶这个引用 16 楼 zhoufoxcn 的回复:
HTML页面代码:
HTML code <formaction="a.aspx" method="post" enctype="multipart/form-data"> <inputtype="file"/> <inputtype="submit" value="upload"/> </form>

处理上传页面的页面代码:
C# code
HttpFileCollection files= HttpContext.Current.Request.Files;
                HttpPostedFile postedFile= files["Resource"];if (postedFile.ContentLength>3072000) FM.Common.Windows.ShowPre("文件大小不能大于300K!");string fileName, fileExtension;
                fileName= System.IO.Path.GetFileName(postedFile.FileName);string SaveFilePath="";if (fileName!="")
                {
                    SaveFilePath="/UploadFile/"+System.DateTime.Now.ToString("yyyymmddhhmmss")+".rar";//SaveFilePath=System.DateTime.Now.ToString("yyyymmddhhmmss") + ".rar";                    fileExtension= System.IO.Path.GetExtension(fileName).ToLower();if (fileExtension!=".rar") FM.Common.Windows.ShowPre("文件格式不正确,你只能上传WinRar压缩包格式文件!");//postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("UploadFile/") + SaveFilePath);                    postedFile.SaveAs(Server.MapPath(SaveFilePath));
                }

[/Quote]
支持这种做法
TalesTen 2010-01-07
  • 打赏
  • 举报
回复
顶这个[Quote=引用 16 楼 zhoufoxcn 的回复:]
HTML页面代码:
HTML code<formaction="a.aspx" method="post" enctype="multipart/form-data"><inputtype="file"/><inputtype="submit" value="upload"/></form>

处理上传页面的页面代码:
C# code
HttpFileCollection files= HttpContext.Current.Request.Files;
HttpPostedFile postedFile= files["Resource"];if (postedFile.ContentLength>3072000) FM.Common.Windows.ShowPre("文件大小不能大于300K!");string fileName, fileExtension;
fileName= System.IO.Path.GetFileName(postedFile.FileName);string SaveFilePath="";if (fileName!="")
{
SaveFilePath="/UploadFile/"+System.DateTime.Now.ToString("yyyymmddhhmmss")+".rar";//SaveFilePath=System.DateTime.Now.ToString("yyyymmddhhmmss") + ".rar"; fileExtension= System.IO.Path.GetExtension(fileName).ToLower();if (fileExtension!=".rar") FM.Common.Windows.ShowPre("文件格式不正确,你只能上传WinRar压缩包格式文件!");//postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("UploadFile/") + SaveFilePath); postedFile.SaveAs(Server.MapPath(SaveFilePath));
}
[/Quote]
antony1029 2010-01-07
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 zhoufoxcn 的回复:]
HTML页面代码:
HTML code<formaction="a.aspx" method="post" enctype="multipart/form-data"><inputtype="file"/><inputtype="submit" value="upload"/></form>

处理上传页面的页面代码:
C# code
HttpFileCollection files= HttpContext.Current.Request.Files;
HttpPostedFile postedFile= files["Resource"];if (postedFile.ContentLength>3072000) FM.Common.Windows.ShowPre("文件大小不能大于300K!");string fileName, fileExtension;
fileName= System.IO.Path.GetFileName(postedFile.FileName);string SaveFilePath="";if (fileName!="")
{
SaveFilePath="/UploadFile/"+System.DateTime.Now.ToString("yyyymmddhhmmss")+".rar";//SaveFilePath=System.DateTime.Now.ToString("yyyymmddhhmmss") + ".rar"; fileExtension= System.IO.Path.GetExtension(fileName).ToLower();if (fileExtension!=".rar") FM.Common.Windows.ShowPre("文件格式不正确,你只能上传WinRar压缩包格式文件!");//postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("UploadFile/") + SaveFilePath); postedFile.SaveAs(Server.MapPath(SaveFilePath));
}
[/Quote]
呵呵。这个可以。还可以用ajax 用a.aspx当后台处理。
周公 2010-01-07
  • 打赏
  • 举报
回复
HTML页面代码:

<form action="a.aspx" method="post" enctype="multipart/form-data">
<input type="file" />
<input type="submit" value="upload" />
</form>


处理上传页面的页面代码:

HttpFileCollection files = HttpContext.Current.Request.Files;
HttpPostedFile postedFile = files["Resource"];
if (postedFile.ContentLength > 3072000) FM.Common.Windows.ShowPre("文件大小不能大于300K!");
string fileName, fileExtension;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
string SaveFilePath = "";
if (fileName != "")
{
SaveFilePath="/UploadFile/"+System.DateTime.Now.ToString("yyyymmddhhmmss") + ".rar";
//SaveFilePath=System.DateTime.Now.ToString("yyyymmddhhmmss") + ".rar";
fileExtension = System.IO.Path.GetExtension(fileName).ToLower();
if (fileExtension != ".rar") FM.Common.Windows.ShowPre("文件格式不正确,你只能上传WinRar压缩包格式文件!");
//postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("UploadFile/") + SaveFilePath);
postedFile.SaveAs(Server.MapPath(SaveFilePath));
}

小_虎 2010-01-07
  • 打赏
  • 举报
回复
ajax
小_虎 2010-01-07
  • 打赏
  • 举报
回复
ajax
fhjzgsy 2010-01-07
  • 打赏
  • 举报
回复
用框架页引用.aspx页上传
wawalike 2010-01-07
  • 打赏
  • 举报
回复
楼上说的不错,你们都没懂我意思啊,我没讲是在aspx页面中,我要的是在html页面中。
daichenghua 2010-01-07
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 ls765029155 的回复:]
HTML code<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="test_Default2"%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title>无标题页</title><scripttype="text/javascript">//动态预览图片function ShowImg(filePath)
{if(filePath!=null)
{
document.getElementById('imgView').innerHTML="<img src='"+filePath.value+"' width='250px' height='200px' style='border:6px double #ccc'>";
}else
{
document.getElementById('imgView').innerHTML="";
}

}function addFileControl()
{var str='<br /><INPUT type="file" NAME="File">'
document.getElementById('FileCollection').insertAdjacentHTML("beforeEnd",str)
}</script><!--样式表开始---><styletype="text/css">
#tab{
border:1px solid #a4d140;
border-collapse:collapse;
width:400px;}
#tab th{
border:1px solid #aaaaaa;
background:#cce98b;
border-collapse:collapse;
text-align:left;}
#tab td{
border:1px solid #cce98b;
border-collapse:collapse;}
.txt{
border:1px solid #888;
background:#ECE9D8;}
.checkBg{
border:1px solid #a4d140;
background:#ECE9D8;}
.button{
border:1px solid #a4d140;margin:5px 0px;
width:80px;background:#cce98b;height:22px;line-height:22px;}</style><stylemce_bogus="1">#tab{
border:1px solid #a4d140;
border-collapse:collapse;
width:400px;}
#tab th{
border:1px solid #aaaaaa;
background:#cce98b;
border-collapse:collapse;
text-align:left;}
#tab td{
border:1px solid #cce98b;
border-collapse:collapse;}
.txt{
border:1px solid #888;
background:#ECE9D8;}
.checkBg{
border:1px solid #a4d140;
background:#ECE9D8;}
.button{
border:1px solid #a4d140;margin:5px 0px;
width:80px;background:#cce98b;height:22px;line-height:22px;}</style><!----样式结束------><scripttype="text/javascript">var count=5;//控制行数var row=0;//添加行function Add()
{if(row== count)
{return;
}var insertTab= document.getElementById("tab").insertRow(-1);var inputTd= document.createElement("td");
inputTd.innerHTML='<input type="file" id="File1" name="File1" size="30" runat="server" onchange="ShowImg(this) />';var delTd= document.createElement("td");
delTd.innerHTML='<input name="delx" type="button" class="button" onclick="Del();" value="删除" />';//把列添加到表 insertTab.appendChild(delTd);
insertTab.appendChild(inputTd);//行递增 row= row+1;
}//删除行function Del()
{var delTab= document.getElementById("tab").rows;var pp= event.srcElement;for(var i=0; i< delTab.length; i++)
{if(pp== delTab[i].cells[0].getElementsByTagName("input")[0])
{
document.getElementById("tab").deleteRow(i);//行递减 row= row-1;
}
}
}</script></head><body><formid="form1" runat="server"><div><divid="msg"></div><inputid="add" type="button" class="button" value="添加" onclick="Add();"/><tableid="tab"><tr><thstyle="width: 100px;"></th><thstyle="width: 300px">
文件</th></tr><tr><td><divid="imgView"></div></td></tr></table><br/><asp:ButtonID="btnUpLoad" runat="server" CssClass="button" OnClick="btnUpLoad_Click"
Text="上传"/><br/><br/><br/><asp:FileUploadID="FileUpload1" runat="server"/><br/><asp:FileUploadID="FileUpload2" runat="server"/><br/><asp:FileUploadID="FileUpload3" runat="server"/><br/><inputid="File1" type="file"/><asp:FileUploadID="FileUpload4" runat="server"/></div></form></body></html>

C# codeusing System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.IO;using System.Text;//用StringBuilder需要导入using System.Drawing;//导入图片操作的命名空间publicpartialclass test_Default2 : System.Web.UI.Page
{
StringBuilder builder=new StringBuilder();protectedvoid Page_Load(object sender, EventArgs e)
{

}//上传按钮protectedvoid btnUpLoad_Click(object sender, EventArgs e)
{
HttpFileCollection files= HttpContext.Current.Request.Files;if (files.Count<=0)
{return;
}for (int i=0; i<files.Count; i++)
{if (!UpFile(files[i], i))
{
}
}

Response.Write("要保存到数据库的字符串:"+ builder.ToString());string[] str;
str= builder.ToString().Split('@');for (int i=0; i< str.Length; i++)
{
Response.Write("截取后的字符串:"+str[i].ToString()+"<br/>");
}

}//上传图片privatebool UpFile(HttpPostedFile file,int index)
{if (!string.IsNullOrEmpty(file.FileName))
{try
{//逻辑:从上传的文件的文件流中获得到文件信息,在内存中重构这张图片//如果图片重构成功则是一张正常的图片,重构失败就不是图片文件//这个操作会导致异常 一定要放到try中去做 Bitmap a=new Bitmap(file.InputStream);string fileName= DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss-fffff")+"-product"+".jpg";
builder.Append(fileName);
builder.Append("@");string path= Server.MapPath("~/Upload")+@"\"+ fileName;//file.SaveAs(path); Response.Write(string.Format("第{0}行{1} 上传成功!<br/>",index+1,file.FileName));
}catch
{
Response.Write(string.Format("第{0}行{1} 上传的文件不合法!<br/>", index+1, file.FileName));returnfalse;
}returntrue;

}else
{
Response.Write(string.Format("第{0}行{1} 文件路径不能为空!<br/>", index+1, file.FileName));returnfalse;
}
}
}
这是前一阵子自己做的一个多图片上传
楼主参考一下
[/Quote]

支持
mohugomohu 2010-01-07
  • 打赏
  • 举报
回复
大哥们,人家是说在html上面上传,你们都发些后台的干什么。
题目都不看啊!!
wangyingnj 2010-01-07
  • 打赏
  • 举报
回复
<tr>
<td style="width: 197px; text-align: right;">
上传图片:</td>
<td style="text-align: left; width: 371px;">
<asp:FileUpload ID="fulUpLoad" runat="server" />
<asp:Button ID="btnUpLoad" runat="server" Text="上传" onclick="btnUpLoad_Click" />
<asp:RegularExpressionValidator ID="revUpload" runat="server"
ControlToValidate="fulUpLoad" Display="Dynamic" ErrorMessage="*文件名后缀名必须是jpg格式"
ValidationExpression="jpg$"></asp:RegularExpressionValidator>
</td>
</tr>


后台代码是

string uploadFileName = Server.MapPath("~/Portal/image/BookCovers/") + txtISBN.Text.Trim() + ".jpg";//获取上传文件的网站目录路径
fulUpLoad.PostedFile.SaveAs(uploadFileName);//储存
imgBook.ImageUrl = "~/Portal/image/BookCovers/" + txtISBN.Text + ".jpg";
ls765029155 2010-01-07
  • 打赏
  • 举报
回复
上面有些多余的测试代码
楼主用的时候修改一下
加载更多回复(4)

62,072

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧