我正在尝试进行多文件上传。在此博客的帮助下,但出现错误。
编码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="Upload_Multiple_Files._default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script language="javascript" type="text/javascript">
var selectedFiles = '';
function ReceiveServerData(response) {
alert(response);
}
function uploadFile() {
var fileList = document.getElementById("fileDivBox").getElementsByTagName("INPUT");
for (i = 0; i < fileList.length; i++) {
selectedFiles += fileList[i].value + "|";
}
CallServer(selectedFiles, '');
}
function attachFile() {
var fu = document.createElement("INPUT");
fu.type = "file";
var br = document.createElement("<BR>");
document.getElementById("fileDivBox").appendChild(fu);
document.getElementById("fileDivBox").appendChild(br);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<a href="#" onclick="attachFile()">Attach a file</a>
</div>
</form>
</body>
</html>
代码隐藏:
namespace Upload_Multiple_Files
{
public partial class _default : System.Web.UI.Page
{
public void RaiseCallbackEvent(string eventArgument)
{
string[] files = (eventArgument.TrimEnd('|')).Split('|');
WebClient client = new WebClient();
foreach (string file in files)
{
client.UploadFile("http://localhost:3850/FileServer.aspx", "POST", file);
}
}
protected void Page_Load(object sender, EventArgs e)
{
string path = @"C:\UploadedFiles\"; // server folder
string[] keys = Request.Files.AllKeys;
foreach (String key in keys)
{
HttpPostedFile file = Request.Files[key];
file.SaveAs(path + file.FileName);
}
}
}
}
错误发生在行br = document.createElement("<BR>");
中:它说“未处理的异常”。我是 javascript 新手,因此没有任何线索是错误的。