日期:2014-05-17  浏览次数:20472 次

上传图在后台应该用什么方法取得 input type="file" 值
.aspx
<table>
<tr>
<td>
 <input id="txtUrl" name="txtUrl" runat="server" type="file"/>

</td>
</tr>
</table>
<table><tr>
  <td>&nbsp;</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 值呢

------解决方案--------------------
直接hidden1.value=document.getElementById("file1").value
隐藏域
------解决方案--------------------
txtUrl.PostedFile.SaveAs("d:\name.jpg")
------解决方案--------------------
this.txtUrl.Value
------解决方案--------------------
C# code

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;
      }

------解决方案--------------------
HttpPostedFile xx=request.Files["前台控件名"]
------解决方案--------------------

HttpFileCollection xx = Request.Files
HttpPostedFile yy=xx[0]

------解决方案--------------------
HTML code

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