I'm trying to make a multi-file upload. with help from this blog, but is getting an error.
the code:
<%@ 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>
codebehind:
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);
}
}
}
}
the error occours in the line: br = document.createElement("<BR>");
where it says "unhandled exeption". I'm new to javascript thereby having no clue was wrong.
The document.CreateElement function will add the < > part of the tag for you. What your code is attempting to do is create an element of <<br>>
, which is invalid. Call the method with just the tag name of BR:
br = document.CreateElement("BR");
This will create a <br>
tag for you as you expect.