前台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控件中的图片地址。
查了好多帖子都没解决
<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控件中的图片地址。
查了好多帖子都没解决
解决方案 »
- 如何动态设置对象属性值
- 在asp里怎么用代码做“当前位置”这一模块。
- Detailsview删除报错:类型“System.Int64”的对象无法转换为类型“System.Int32”。
- 菜鸟请教:导航菜单怎么实现???
- GridView注册脚本问题.直接运行脚本注册没有成功.但是调试的时候设置断点就会成功!
- 问一个在面试时被问到的问题
- 能告诉我做考试系统的思路吗?
- 存储过程 加 jquery实现分页
- Microsoft Visual SourceSafe签出问题,求解!
- table 的td中间有空格
- 求高人帮助,写一个JavaScript特效...
- 简单问题,databind.eval中如何做条件判断?
<!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'> <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>
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());
}
}
}
用ViewState也保存不住?
<form enctype="multipart/form-data" runat="server">
...
</form>之后你就从Request.Files里面获取上传的文件吧。