请大家给一些“上传文件”的代码(要C#的)

mhlxhm 2003-08-26 10:46:20
请大家给一些“上传文件”的代码(要C#的)
...全文
43 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
alphawin 2003-09-01
  • 打赏
  • 举报
回复
给过你了 啊 ,在你邮箱
cnhgj 2003-08-26
  • 打赏
  • 举报
回复
http://www.codeproject.com/aspnet/netimageupload.asp?target=upload
panyee 2003-08-26
  • 打赏
  • 举报
回复
数据库有这些字段: id, imgData, type, description, imgSize
id是自增长

<%@Page language="C#" %>
<%@import namespace="System.IO"%>
<%@import namespace="System.Data"%>
<%@import namespace="System.Data.SqlClient"%>
<script language="C#" runat="server">
public void Button_Submit(Object o, EventArgs e)
{
HttpPostedFile upFile = up_file.PostedFile;
int iFileLength = upFile.ContentLength;
try
{
if(iFileLength == 0)
{
txtMess.Text = "请选择要上传的文件!";
}
else
{
Byte[] FileByteArray = new Byte[iFileLength];
Stream StreamObject = upFile.InputStream;
StreamObject.Read(FileByteArray, 0, iFileLength);
SqlConnection conn = new SqlConnection("server=yy;uid=sa;pwd=;database=pany");
string sql = "insert into t_imgs (imgData, type, description, imgSize) values "
+ "(@Image, @ContentType, @ImageDescription, @ImgSize)";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.Add("@Image", SqlDbType.Binary, iFileLength).Value = FileByteArray;
cmd.Parameters.Add("@ContentType", SqlDbType.VarChar, 50).Value = upFile.ContentType;
cmd.Parameters.Add("@ImageDescription", SqlDbType.VarChar, 200).Value = txtDesc.Text;
cmd.Parameters.Add("@ImgSize", SqlDbType.BigInt, 8).Value = upFile.ContentLength;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
txtDesc.Text = "";
txtMess.Text = "OK!你已经成功上传了类型的文件";
}
}
catch(Exception ex)
{
txtMess.Text = ex.Message.ToString();
}
}



</script>

<html>
<head>
<title>上传图片</title>
</head>
<body bgcolor="#FFFFFA">
<form enctype="multipart/form-data" runat="server" id="form1">
<table runat="server" width=700 align=left id="table1" cellpadding=0 cellspacing =0 border=0>
<tr>
<td>上传图片</td>
<td>
<input type="file" id="up_file" runat="server" style="width:320" accept="text/*" name="up_file">
</td>
</tr>
<tr>
<td>文件说明</td>
<td>
<asp:TextBox runat="server" width=230 id="txtDesc" maintanstate="false" />
</td>
</tr>
<tr>
<td>
<asp:label runat="server" id="txtMess" forecolor=red maintainstate="false" />
</td>
<td>
<asp:Button runat="server" width=230 onclick="Button_Submit" text="上传" />
</td>
</tr>
</table>
</form>
</body>
</html>

mhlxhm 2003-08-26
  • 打赏
  • 举报
回复
最好有完整的例子。谢谢!
asam2183 2003-08-26
  • 打赏
  • 举报
回复
if(fileUpLoad.PostedFile.ContentLength==0)
return false;
string sFileName= fileUpLoad.PostedFile.FileName ;
//文件名
sFileName =Path.GetFileName(sFileName);
//保存路径
string scFileName_Old=Path.GetFileNameWithoutExtension(sFileName);
string sDirFile=Server.MapPath(".")System.Configuration
.ConfigurationSettings.AppSettings["FTPDir"].ToString(); //"\\Attachmant\\";

if(!Directory.Exists(sDirFile))
Directory.CreateDirectory(sDirFile);
string sFileNameNew=sFileName;
sFileNameNew=scFileName_Old+sNewID+Path.GetExtension(sFileName).ToString();
fileUpLoad.PostedFile.SaveAs(sDirFile+sFileNameNew) ;
//判断文件是否上传成功
if (!File.Exists(sDirFile+sFileNameNew))
return false;
csharplove 2003-08-26
  • 打赏
  • 举报
回复
http://www.mindcracker.com/csharp/asp/fileupload.zip

HttpPostedFile
qimini 2003-08-26
  • 打赏
  • 举报
回复
//附件的名称
private string AttachmentName="";
//附件的二进制数据流
private Stream AttachmentStream=null;
//附件的MIME类型
private string AttachmentContentType="";
//附件的大小
private int AttachmentContentLength;
// 添加时间
private DateTime AddTime=DateTime.Now;


mlInputFile p_InputFileObj=new HtmlInputFile();

string Temp=p_InputFileObj.PostedFile.FileName;
Temp=Temp.Substring(Temp.LastIndexOf(@"\")+1);
this.AttachmentName=Temp;
this.AttachmentStream=p_InputFileObj.PostedFile.InputStream;
this.AttachmentContentType=p_InputFileObj.PostedFile.ContentType;
his.AttachmentContentLength=p_InputFileObj.PostedFile.ContentLength;

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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