asp.net 中上传文件用什么啊?

yuna103 2004-10-28 10:24:37
有代码吗
...全文
146 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
delphiseabird 2004-10-28
  • 打赏
  • 举报
回复
httppostedfile类
goody9807 2004-10-28
  • 打赏
  • 举报
回复
<input type=file id=file1 ruanat=server>
trendvb 2004-10-28
  • 打赏
  • 举报
回复
代码如下,接分


private void btn_PostUp_Click(object sender, System.EventArgs e)
{
string Path;
string FileName;
int Po;
long FileSize;
string fType;
if(File1.PostedFile.FileName.Trim()!="") //PostFile's Filename is not empty
{
try
{
Path=Server.MapPath("\\BookShop\\UpFile").ToString();
FileName=File1.PostedFile.FileName.ToString();
fType=File1.PostedFile.ContentType;
FileSize=File1.PostedFile.ContentLength;
if(fType.Substring(0,5)!="image")
{
ShowErrMsg("PostFile is not Image!");
return;
}
Po=FileName.LastIndexOf((char)92);
FileName=FileName.Substring(Po+1,FileName.Length -Po-1);
ViewState["PostFileName"]=FileName; //Remember PostFile's name
File1.PostedFile.SaveAs(Path+"\\"+FileName);
img_BookImg.ImageUrl="UpFile\\"+FileName;
}
catch(Exception er)
{
ShowErrMsg(er.Message);
}
}
}
baihecheng 2004-10-28
  • 打赏
  • 举报
回复
up
maomhz 2004-10-28
  • 打赏
  • 举报
回复
aspx:
<%@ Page Inherits="WebPortal.Upload" Language="C#" src="cs/sc.cs" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>作品图例上传</title>
<script language="JavaScript">
function addFile()
{
var str = '<INPUT type="file" size="50" NAME="File">'
document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str)
}
</script>
</HEAD>
<body>
<link href="css/css.css" rel="stylesheet" type="text/css">
<form id="form1" method="post" runat="server" enctype="multipart/form-data">
<div align="center">
<TABLE class=tableborder1 cellSpacing=1 cellPadding=1 align=center>
<TBODY>
<TR>
<TH vAlign=center align=left width="100%" colSpan=2 style="font-weight:normal" id=tabletitlelink
height=25 >=><a href="index.aspx"> <b>网站管理系统<b></a>=><a href="product.aspx"> <b>作品管理<b></a>=>作品图例上传 </TH></TR>
<TR>
<TD class=tablebody1 vAlign=top noWrap>
<P id="MyFile"><INPUT type="file" size="30" NAME="File"></P>
</td></tr>
<TR>
<TD class=tablebody1 vAlign=top noWrap>
<font color=#FF0000>* 请先选择作品图例,请用gif或者jpg格式。</font>
<TR>
<TD class=tablebody1 vAlign=top noWrap>
<input onclick="this.form.reset()" type="button" value="重置(ReSet)">
<asp:Button Runat="server" Text="开始上传" ID="UploadButton"></asp:Button>
</td> </tr>
</table>
<P>
<asp:Label id="strStatus" runat="server" Font-Names="宋体" Font-Bold="True" Font-Size="9pt"
Width="500px" BorderStyle="None" BorderColor="White">
</asp:Label>
<br>
</P>
</div>
</form>
</body>
</HTML>

cs:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace WebPortal
{
/// <summary>
/// UpLoad 的摘要说明。
/// 实现多文件上传
/// </summary>
public class Upload : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button UploadButton;
protected System.Web.UI.WebControls.Label strStatus;

private void Page_Load(object sender, System.EventArgs e)
{
/// 在此处放置用户代码以初始化页面
if (this.IsPostBack) this.SaveImages();
}

private Boolean SaveImages()
{
///'遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;
/// '状态信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
strMsg.Append("上传的文件是:<hr color=red>");
try
{
for(int iFile = 0; iFile < files.Count; iFile++)
{
///'检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string fileName, fileExtension,fileLength;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName != "")
{
fileExtension = System.IO.Path.GetExtension(fileName);
strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
strMsg.Append("上传文件的文件名:" + fileName + "<br>");
strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr>");
if((fileExtension.IndexOf("jpg")==-1)&(fileExtension.IndexOf("gif")==-1)&(fileExtension.IndexOf("jpeg")==-1))
{ Session["error"]= "只能上传jpg和gif格式的文件";
Response.Redirect("error.aspx"); }
///'可根据扩展名字的不同保存到不同的文件夹
///注意:可能要修改你的文件夹的匿名写入权限。

Random R = new Random();
int RecNo = R.Next(1000000);
string rec=Convert.ToString(RecNo);
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("up/") + rec+".gif");
Bitmap img = new Bitmap(Server.MapPath("up/") + rec+".gif");//上传图片的地址
int a= img.Size.Width; // 宽
if(a>550){
Session["error"]= "您上传的图片太大了。";
Response.Redirect("error.aspx");
}
Session["img"]=rec;
Response.Redirect("cpfabu.aspx");
}
}
strStatus.Text = strMsg.ToString();
return true;
}
catch(System.Exception Ex)
{
strStatus.Text = Ex.Message;
return false;
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.ID = "Upload";
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}

ytywj2003 2004-10-28
  • 打赏
  • 举报
回复
<input type=file id=file1 ruanat=server>
yuna103 2004-10-28
  • 打赏
  • 举报
回复
最一般用什么方法呢
wudixiaocaoren 2004-10-28
  • 打赏
  • 举报
回复
用控件吧 upload.dll 在www.sorke.com有的下载。

62,042

社区成员

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

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

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

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