上传图在后台应该用什么方法取得 input type="file" 值

baiyashan 2010-01-08 01:22:03
.aspx
<table>
<tr>
<td>
<input id="txtUrl" name="txtUrl" runat="server" type="file"/>

</td>
</tr>
</table>
<table><tr>
<td> </td>
<td><asp:UpdatePanel ID="UpdatePanel1" runat="server"><ContentTemplate><input name="Submit2" id="Submit2" type="button" class="anniu01" value="提交" onserverclick="btnSubmit_Click" runat="server"/></ContentTemplate><Triggers>
<asp:AsyncPostBackTrigger ControlID="Submit2" >
</asp:AsyncPostBackTrigger>
</Triggers>
</asp:UpdatePanel></td>
</tr>
</table>


.aspx.cs
用了.
Request.Form["txtUrl"] 不好使呀.
在后台应该用什么方法取得 input type=file 值呢
...全文
461 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
易酷天 2012-09-19
  • 打赏
  • 举报
回复
给 <input type='file' ruant='server' 转换为服务器控件,之后使用ID获得里面的值即可
baiyashan 2010-01-08
  • 打赏
  • 举报
回复
换一个页面也是不行...各位不信把我的代码粘过去看看...吧.真不行.楼上的所有方法都试过了.都不行.
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 baiyashan 的回复:]
我不知道是不是我用了 AJAX 控件的原因 <asp:UpdatePanel ID="UpdatePanel1" runat="server">
[/Quote]
應該不是的...你的 input控件沒放在Ajax控件上面
建議你單獨新建一個項目試試
baiyashan 2010-01-08
  • 打赏
  • 举报
回复
我不知道是不是我用了 AJAX 控件的原因<asp:UpdatePanel ID="UpdatePanel1" runat="server">
  • 打赏
  • 举报
回复

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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 runat="server">
</head>
<body>
<form id="form1" runat="server">


<input id="File1" runat="server" type="file" />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" />

</form>
</body>
</html>



protected void Button2_Click(object sender, EventArgs e)
{//瀏覽後 點擊Button2輸出
Response.Write(this.File1.Value);
}
newdigitime 2010-01-08
  • 打赏
  • 举报
回复

HttpFileCollection xx = Request.Files
HttpPostedFile yy=xx[0]
baiyashan 2010-01-08
  • 打赏
  • 举报
回复
大家别忘了 input 的 type=file
newdigitime 2010-01-08
  • 打赏
  • 举报
回复
HttpPostedFile xx=request.Files["前台控件名"]
baiyashan 2010-01-08
  • 打赏
  • 举报
回复
用 Value 是空值.
takako_mu 2010-01-08
  • 打赏
  • 举报
回复

HttpFileCollection files = HttpContext.Current.Request.Files;
try
{
for(int iFile = 0; iFile < files.Count; iFile++)
{
///'检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string fileName, fileExtension;
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>");
///'可根据扩展名字的不同保存到不同的文件夹
///注意:可能要修改你的文件夹的匿名写入权限。
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("images/") + fileName);
}
}
strStatus.Text = strMsg.ToString();
return true;
}
catch(System.Exception Ex)
{
strStatus.Text = Ex.Message;
return false;
}

  • 打赏
  • 举报
回复
[Quote=引用 4 楼 jijunwu 的回复:]
this.txtUrl.Value
[/Quote]
是HTMl控件轉換為在服務器端運行的
所以獲取值的時候用 Value屬性
  • 打赏
  • 举报
回复
this.txtUrl.Value
baiyashan 2010-01-08
  • 打赏
  • 举报
回复
2楼的方法用了不好使.
yhy80 2010-01-08
  • 打赏
  • 举报
回复
txtUrl.PostedFile.SaveAs("d:\name.jpg")
happy664618843 2010-01-08
  • 打赏
  • 举报
回复
直接hidden1.value=document.getElementById("file1").value
隐藏域