好可怜啊,没有人帮我解答!

wyz52126 2003-08-21 04:04:04
我想做文件上传,用了File控件,可是为什么我一点上传按钮,File里的
路径就清空了呢?大哥们,指点一下小弟我啊!先谢谢了!
...全文
55 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
stuhome 2003-08-21
  • 打赏
  • 举报
回复
如果
<INPUT id="File1" type="file" size="38" name="File1" runat="server">被申明为服务器控件(即runat=server)且*.cs文件有以下申明:“protected System.Web.UI.HtmlControls.HtmlInputFile File1;”就可以不用加enctype="multipart/form-data",.NET Framework在生成 web页面时会自动加上去。
feiyu21 2003-08-21
  • 打赏
  • 举报
回复
在<form > 里有加上enctype="multipart/form-data"
wyz52126 2003-08-21
  • 打赏
  • 举报
回复
为什么 File1.PostedFile.FileName 的值就是为空呢?
而 File1.Value 却不为空呢?是不是什么地方的属性要设置一下的啊?
大哥们,快指教一下啊,1个小时后就下班了,我必须要弄好的呀!
stuhome 2003-08-21
  • 打赏
  • 举报
回复
哇靠,这还不够吗?
wyz52126 2003-08-21
  • 打赏
  • 举报
回复
HttpPostedFile upFile = up_file.PostedFile; 虽然不错!
但是我的程序里 up_file.PostedFile 就是为空啊!好生郁闷啊!
选了也是空!对了, stuhome(桶) 谢谢你哦,辛苦了!有下载代码吗?
wyz52126 2003-08-21
  • 打赏
  • 举报
回复
panyee(快乐王子) :
兄台,先谢谢了,我试试看哦!对了,你写过上传,那你也肯定写过下载啦?
顺便和小弟我讲一下啊!谢谢!(C#)
stuhome 2003-08-21
  • 打赏
  • 举报
回复
提供给你一个源程序,我写的。
///////////////上传界面/file_upload.aspx/////////////////
…………
<form id="Form1" method="post" runat="server">
<font color="red">上传文件</font><br>
<br>
<INPUT id="File1" type="file" size="38" name="File1" runat="server"><BR>
<b>小于2M</b> <font color="#666666">(为了方便管理,您上传的文件将被重命名)</font><BR>
<br>
<b>文件说明:</b>20个字符以内<br><font color=red>可以不填,</font>若不填则取原文件名。<br>
<asp:textbox id="TextBox_file_intro" runat="server" Width="283px" MaxLength="20"></asp:textbox><BR>
<asp:label id="Label_hint" runat="server"></asp:label><BR>
<asp:button id="Button_uploadfile" runat="server" Text="上传文件"></asp:button></form>
…………
//////////////////////file_upload.aspx.cs///////////////////////////
…………

protected System.Web.UI.HtmlControls.HtmlInputFile File1;
protected System.Web.UI.WebControls.Button Button_uploadfile;
protected System.Web.UI.WebControls.Label Label_hint;
protected System.Web.UI.WebControls.TextBox TextBox_file_intro;
public string file=null;

…………
private void Button_uploadfile_Click(object sender, System.EventArgs e)
{
Label_hint.Text="<br><font color=red>"+SaveFiles()+"</font><br>";
}
…………
private string SaveFiles()
{
string file_extension_str=conn.GetString("select top 1 file_upload_extension from sys_configure");
string[] file_extension=file_extension_str.Split(',');
bool extention_chk=false;
Random rd=new Random();
System.Web.HttpFileCollection _files = System.Web.HttpContext.Current.Request.Files;
System.Text.StringBuilder _message = new System.Text.StringBuilder("");
if(_files.Count==0)
{
return "请上传一个文件";
}
else
{
try
{
System.Web.HttpPostedFile _postedFile = _files[0];
float file_size=(float)_postedFile.ContentLength/(1024*1024);
if(file_size>2)
return "当前文件为 <b><span style='font-size=20px;font-family=tahoma;color=#666666'>"+file_size+"</span></b> M<br>单个上传文件大小不能超过 <b><span style='font-size=20px;font-family=tahoma;color=#666666'> 2 </span></b>M,你可以考虑上传压缩格式的文件。";
System.String _fileName, _fileExtension;
_fileName = System.IO.Path.GetFileName(_postedFile.FileName);
string intro=_fileName.Substring(0,_fileName.LastIndexOf('.'));
_fileExtension = System.IO.Path.GetExtension(_fileName).ToLower();
if(!(_fileName.Trim().Length==0|_fileExtension.Trim().Length==0))
{
_fileName="AEFile"+"_"+DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString()+DateTime.Now.Day.ToString()+DateTime.Now.Hour.ToString()+DateTime.Now.Minute.ToString()+"_"+rd.Next(10000).ToString()+_fileExtension;
for(int i=0;i<file_extension.Length;i++)
{
if(_fileExtension=="."+file_extension[i])
{
extention_chk=true;
break;
}
}
if (extention_chk)
{
_postedFile.SaveAs(
System.Web.HttpContext.Current.Request.MapPath(
"../uploadfiles/") + _fileName);
string name=null;
if(Session["loginname"]!=null)
{
name=Session["loginname"].ToString();
}
else
{
name="Guest";
}
if(TextBox_file_intro.Text.Trim().Length!=0)
{
intro=TextBox_file_intro.Text;
}
conn.NonQuery("insert into file_upload (intro,name,pub_user,pub_time,file_size) values ('"+intro+"','"+_fileName+"','"+name+"','"+DateTime.Now.ToString()+"','"+file_size*1024+"')");
}
else
{
return "只能上传后缀为<b><span style='font-size=20px;font-family=tahoma;color=#666666'> "+file_extension_str+" </span></b>的文件";
}
}
else
{
return "上传的文件名为空或者后缀为空";
}
}
catch
{
return "图片上传失败,请及时联系系统管理员";
}
}
return "文件上传成功!";
}
…………
//////////////////////////////////////////////////////////
panyee 2003-08-21
  • 打赏
  • 举报
回复

按钮事件写了没有?

用我以前的例子试试, 文件上传到sql数据库里


<%@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>

wyz52126 2003-08-21
  • 打赏
  • 举报
回复
我不是说了吗?我想做个文件上传嘛,File里面的是路径和文件名嘛,没有的话,那还上传什么啦?大哥,你帮帮我啊!我急死了!
acewang 2003-08-21
  • 打赏
  • 举报
回复
你要那个路径做什么

62,041

社区成员

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

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

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

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