<input type=button>触发<input type=file>的click事件后 ,点击保存, 为什么先清空input,而导致上传的文件为空?
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication2.WebForm1" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script language="JavaScript">
var i=0;
function addFileControl()
{
var str = '<INPUT type="file" NAME="File'+i+'" id="_upfile'+i+'">'
document.getElementById('FileCollection').insertAdjacentHTML("beforeEnd",str)
document.getElementById ("_upfile"+i).click();
i++;
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<P align="center"><input onclick="addFileControl()" type="button" value="增加(File)"></P>
<P id="FileCollection"><INPUT type="file" name="File">
<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 152px; POSITION: absolute; TOP: 168px" runat="server"
Text="保存"></asp:Button> </P>
</form>
</body>
</HTML>
后台代码:
using System.Web.UI.HtmlControls;
namespace WebApplication2
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
private void Button1_Click(object sender, System.EventArgs e)
{
string[] inputNames = Request.Files.AllKeys;
for(int i=0;i<inputNames.Length ;i++)
Response.Write (inputNames[i]);
StringBuilder uploadMessage = new StringBuilder("当前上传的文件分别是:<hr color=red>");
string path=Server.MapPath("/");
//上载文件列表中的每一个文件
for (int i = 0; i < inputNames.Length; i++)
{
//if (inputNames[i].IndexOf(file.name)>= 0)
//{
String fileName;
String fileExtension;
//获取上载文件的文件名称
HttpPostedFile postedFile = Request.Files[inputNames[i]];
fileName = Path.GetFileName(postedFile.FileName);
string fi=path+fileName;
if(fileName != null && fileName !="")
{
//获取上载文件的扩展名称
fileExtension = Path.GetExtension(fileName);
uploadMessage.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
uploadMessage.Append("客户端文件地址:" + postedFile.FileName + "<br>");
uploadMessage.Append("上传文件的文件名:" + fileName + "<br>");
uploadMessage.Append("上传文件的扩展名:" + fileExtension + "<br><hr>");
//上载文件
postedFile.SaveAs(fi);
//}
}
}
}
}
} --------------------编程问答-------------------- 什么问题 --------------------编程问答-------------------- 我好像回复过这样的贴,file控件必须用户点击才可,不能使用代码触发.
--------------------编程问答-------------------- /// <summary>
/// 创建文件
/// </summary>
/// <param name="filename"></param>
/// <param name="path"></param>
/// <param name="contents"></param>
public static bool CreateFile(string filename, string path, byte[] contents)
{
try
{
FileStream fs = File.Create(path + "\\" + filename);
fs.Write(contents, 0, contents.Length);
fs.Close();
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 读取文件
/// </summary>
/// <param name="name"></param>
/// <param name="parentName"></param>
public static string OpenText(string parentName)
{
StreamReader sr = File.OpenText(parentName);
StringBuilder output = new StringBuilder();
string rl;
while ((rl = sr.ReadLine()) != null)
{
output.Append(rl);
}
sr.Close();
return output.ToString();
}
/// <summary>
/// 写入一个新文件,在文件中写入内容,然后关闭文件。如果目标文件已存在,则改写该文件。
/// </summary>
/// <param name="name"></param>
/// <param name="parentName"></param>
public static bool WriteAllText(string parentName, string contents)
{
try {
File.WriteAllText(parentName, contents,Encoding.Unicode);
return true;
}
catch {
return false;
}
}
/// <summary>
/// 删除文件
/// </summary>
/// <param name="path"></param>
public static bool DeleteFile(string path)
{
try
{
File.Delete(path);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 移动文件
/// </summary>
/// <param name="oldPath"></param>
/// <param name="newPath"></param>
public static bool MoveFile(string oldPath, string newPath)
{
try
{
File.Move(oldPath, newPath);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 读取文件信息
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static FileSystemItem GetItemInfo(string path)
{
FileSystemItem item = new FileSystemItem();
if (Directory.Exists(path))
{
DirectoryInfo di = new DirectoryInfo(path);
item.Name = di.Name;
item.FullName = di.FullName;
item.CreationDate = di.CreationTime;
item.IsFolder = true;
item.LastAccessDate = di.LastAccessTime;
item.LastWriteDate = di.LastWriteTime;
item.FileCount = di.GetFiles().Length;
item.SubFolderCount = di.GetDirectories().Length;
}
else
{
FileInfo fi = new FileInfo(path);
item.Name = fi.Name;
item.FullName = fi.FullName;
item.CreationDate = fi.CreationTime;
item.LastAccessDate = fi.LastAccessTime;
item.LastWriteDate = fi.LastWriteTime;
item.IsFolder = false;
item.Size = fi.Length;
}
return item;
}
/// <summary>
/// 复制文件夹 5-1-aspx
/// </summary>
/// <param name="source"></param>
/// <param name="destination"></param>
public static bool CopyFolder(string source, string destination)
{
try
{
String[] files;
if (destination[destination.Length - 1] != Path.DirectorySeparatorChar)
destination += Path.DirectorySeparatorChar;
if (!Directory.Exists(destination)) Directory.CreateDirectory(destination);
files = Directory.GetFileSystemEntries(source);
foreach (string element in files)
{
if (Directory.Exists(element))
CopyFolder(element, destination + Path.GetFileName(element));
else
File.Copy(element, destination + Path.GetFileName(element), true);
}
return true;
}
catch
{
return false;
}
} --------------------编程问答-------------------- 参考
http://blog.csdn.net/cpp2017/archive/2009/08/06/4418202.aspx
--------------------编程问答-------------------- 必须人工点击,这是浏览器出于安全角度来考虑的. --------------------编程问答-------------------- 控件属性问题
添加enctype="multipart/form-data"
补充:.NET技术 , .NET技术前瞻