Upload af flere billeder
Hej eksperterJeg forsøger mig med at lade en bruger uploade flere filer.
Nedenfor har I min kode. Det der undrer mig er, at Request.Files.Count antager værdien "0", uanset hvor mange filer jeg angiver til upload.
Kan I forklare hvorfor det sker?
Jeg er først lige begyndt at indarbejde klient side scripts i mine arbejder, så det kan sagtens være her fejlen ligger.
På forhånd tak.
/dresen
web form:
---------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MultipleFileUpload.aspx.cs" Inherits="MultipleFileUpload" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
<!--
////////////////////////////////////////////////////////////////////////
var fileCount = 1;
////////////////////////////////////////////////////////////////////////
function AddFileInput(){
var fileSectionDiv = document.getElementById("files");
var fileItemDiv = document.createElement("div");
//Increment the file counter
fileCount++;
//Set up the HTML content
var content = "<input type=file name=fileUpload" +
fileCount + "> <a href='java script:RemoveFile(" +
fileCount + ");'>Remove</a>"
//Set up the fileItemDiv properties and append element
fileItemDiv.id = "fileItemDiv" + fileCount;
fileItemDiv.innerHTML = content;
fileSectionDiv.appendChild(fileItemDiv);
}
////////////////////////////////////////////////////////////////////////
function RemoveFile(fileIndex){
var fileSectionDiv = document.getElementById("files");
var fileItemDiv = document.getElementById("fileItemDiv" + fileIndex);
fileSectionDiv.removeChild(fileItemDiv);
}
-->
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div id="files">
<input type="file" name="fileUpload1" /><br />
</div>
<input type="button" value="Add File" onclick="AddFileInput();"/>
<asp:Button runat="server" ID="btnUpload" Text="Upload Files" OnClick="btnUpload_Click" />
</div>
</form>
</body>
</html>
code behind:
-----------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class MultipleFileUpload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Page.Form.Enctype = "multipart/form-data;";
}
protected void btnUpload_Click(object sender, EventArgs e)
{
throw new Exception(Request.Files.Count.ToString());
//for (int i = 0; i < Request.Files.Count; i++)
//{
// HttpPostedFile file = Request.Files[i];
// if (!String.IsNullOrEmpty(file.FileName))
// {
// file.SaveAs(@"C:\Temp\" + file.FileName);
// }
//}
}
}