The FileUpload control does not work with AJAX, because it needs a full postback to upload a file. So we have to change the AJAX partial postback to a full postback:
ASPX, add a ScriptManager, UpdatePanel, FileUpload and LinkButton control:
<%@ Page Language="C#" AutoEventWireup="true" Codebehind="Default.aspx.cs" Inherits="WebApplicationCSharp._Default" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<!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>Web app c#</title>
<link rel="stylesheet" type="text/css" href="~/CustomStyles.css">
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManagerTest" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanelTest" runat="server">
<ContentTemplate>
<asp:FileUpload ID="FileUploadTest" runat="server" />
<asp:LinkButton ID="LinkButtonTest" runat="server" OnClick="LinkButtonTest_Click">Test Upload</asp:LinkButton>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
Change Page_Load event
protected void Page_Load(object sender, EventArgs e)
{
// Disable AJAX for the
LinkButtonTest ONLY. Because we are using the FileUpload control, which does not work with AJAX, we need a full PostBack.
LinkButtonTest.Attributes.Add("OnClick", String.Format("form1.__EVENTTARGET.value = ‘{0}’; form1.submit(); return false;", LinkButtonTest.UniqueID));
}
Add Click event on LinkButtonTest
/// <summary>
/// The LinkButtonTest is on an AJAX update panel, but by overriding the OnClick event in the Page_Load event,
/// this event will fire with a full postback.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void LinkButtonTest_Click(object sender, EventArgs e)
{
this.UploadFile();
}
Add an UploadFile function
public void UploadFile()
{
using (SPSite sitecollection = new SPSite("http://localhost"))
{
using (SPWeb web = sitecollection.RootWeb)
{
if (FileUploadTest.HasFile)
{
using (Stream fileContent = FileUploadTest.FileContent)
{
string fileName = FileUploadTest.FileName;
string newListItemImageName = "";
// Get list by name
SPList list = web.Lists[listName];
// Get rootfolder of the list
SPFolder folder = list.RootFolder;
// Delete the current file if it exists. I can’t find a way to check the existence of a file without an exception and not looping through all the items in the list.
bool fileExists = false;
SPFile currentFile = null;
try
{
currentFile = folder.Files[fileName];
fileExists = true;
}
catch { fileExists = false; }
if (fileExists) { currentFile.Delete(); }
// Add file to the list
currentFile = folder.Files.Add(String.Format("{0}/{1}", folder.ServerRelativeUrl, fileName), fileContent);
}
}
}
}
}