前台js:
<script language="javascript" type="text/javascript">
function AddPhoto()
{      var index = Math.floor(Math.random() * 100000000);         var str='<input type=\"file\" name=\"file\" id=\"'+index+'\" style=\"position:absolute;filter:alpha(opacity=0);\" size=\"1\">'
        document.getElementById('aaa').insertAdjacentHTML("beforeEnd",str); 
    document.getElementById(index).click();
    
    var PhotoURL="";
    PhotoURL = document.getElementById(index).value;
    if(PhotoURL == "")
    {
        alert("请选择要上传的图片!");
            return false;
    }
    else
    {
        var str='<img id\"kkk\" src=\"'+PhotoURL+'\" width=\"120\" height=\"120\" />'
            document.getElementById('pic_space_main').insertAdjacentHTML("beforeEnd",str);
    }
}
前台显示代码:
<td colspan="2" align="center"><div id="pic_space_main" 
      style="OVERFLOW-X: hidden; WIDTH: 500px; HEIGHT: 350px" align="left" 
      valign="top"></div></td>
<input type="button"  value="浏览本地图片" class="fg_btn" onclick="AddPhoto();" size=""></td>
                <td width="152" align="center" style="height: 24px"><asp:Button ID="Upload" runat="server" class="fg_btn" Text="确认上传图片" OnClick="Button1_Click" /></td>现在的问题是我上传的时候 保存不了js生成的File控件中的图片地址。
查了好多帖子都没解决

解决方案 »

  1.   

    http://dotnet.aspx.cc/ShowDetail.aspx?id=58EA3515-36F2-4FD9-AC89-EAF49F59816C
      

  2.   

    注意!!!在选择上传图片时  File控件是隐藏的
      

  3.   

    <%@ Page language="c#" Codebehind="MultiAttchments.aspx.cs" AutoEventWireup="false" Inherits="WebApplication3.MultiAttchments" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <script>
    function AddAttachments()
    {
    document.getElementById('attach').innerText = "继续添加附件";

    tb = document.getElementById('attAchments');
    newRow = tb.insertRow();
    newRow.insertCell().innerHTML = "<input name='File' size='50' type='file'>&nbsp;&nbsp;<input type=button value='删除' onclick='delFile(this.parentElement.parentElement.rowIndex)'>";
    }
    function delFile(index)
    {
    document.getElementById('attAchments').deleteRow(index);
    tb.rows.length > 0?document.getElementById('attach').innerText = "继续添加附件":document.getElementById('attach').innerText = "添加附件";
    }
    </script>
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="form1" method="post" runat="server" enctype="multipart/form-data">
    <div><table id="attAchments"></table></div><span><IMG src="icoAddFl.gif"> </span> <A id="attach" style="font-family:宋体;font-size:9pt;" title="如果您要发送多个附件,您只需多次点击“继续添加附件”即可, 要注意附件总量不能超过发送限制的大小。" onclick="AddAttachments();"
    href="javascript:;" name="attach">添加附件</A>
    <br><br><br><br><br><br>
    <asp:Button id="btnSend" runat="server" Text=" 上传 "></asp:Button>
    </form>
    </body>
    </HTML>
      

  4.   

    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;
    using System.Text;namespace WebApplication3
    {
    /// <summary>
    /// Summary description for WebForm7.
    /// </summary>
    public class MultiAttchments : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Button btnSend;

    private void Page_Load(object sender, System.EventArgs e)
    {
    } #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {    
    this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregionprivate void btnSend_Click(object sender, System.EventArgs e)
    {
    StringBuilder sb = new StringBuilder(); int attCount = 0;
    string filePath = "";
    for(int i=0; i< Request.Files.Count; i++)
    {
    if(Request.Files[i].ContentLength > 0)
    {
    filePath = Request.Files[i].FileName;
    sb.Append("Files" + attCount++ + ": " + filePath + "<br>");
    Request.Files[0].SaveAs(Server.MapPath("./") + filePath.Substring(filePath.LastIndexOf("\\")+1));
    }
    } sb.Insert(0, "you upload " + attCount + " files.<br>");
    Response.Write(sb.ToString());
    }
    }
    }
      

  5.   

    感谢大家的热心   关键是File控件要隐藏的  点击浏览图片就直接可以选择图片而不是在生成的File控件中选图片,  现在的问题是  不直接点File控件选择图片, 上传的时候生成的File控件中的值保存不住
      

  6.   

    上传的时候生成的File控件中的值保存不住-------------------------
    用ViewState也保存不住?
      

  7.   

    可以看一下我上面的代码, 选择图片后  点击上传 会先把File控件中的值清空
      

  8.   

    在服务器端你是不是没放任何FileUpload控件?这样需要手动为<form>加一个属性:
    <form enctype="multipart/form-data" runat="server">
      ...
    </form>之后你就从Request.Files里面获取上传的文件吧。
      

  9.   

    现在根本传不到后台,  前台点上传刷新就会把File控件中的值清空