Using multi-upload control (STSUpld.UploadCtl) in Custom SharePoint Page
**Introduction:
**
UploadCtl is an ActiveX control shipped with MSOffice 2003 which allows multiple documents to be uploaded from an external application to a document library on a site in Microsoft Windows SharePoint Services.
So, in this way we can upload multiple files at a time in any Document Library.
Now, the Question is can we use the same functionality in our custom pages. The answer is yes, we can use this feature. I have still unable to use this Active X control in ASP.Net application. But I have used it in SharePoint Application.
**Solution:
**if we look at the URL of above Page(Image), it is like
Upload.aspx page uses these following querystring to upload the file. If we decode it we find that
List= ListID
Root Folder= /sites/SC1/S1/Pages
Source= http://teseserver/sites/SC1/S1/Pages/Forms/AllItems.aspx //Redirect the page here after upload
MultipleUpload=1 //It shows we need to open Multiple Upload Page
So, we can redirect the page with those above QueryStrings.
The Design Page:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>
<!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 id="Head1" runat="server">
<title>Multi Upload</title>
</head>
<body>
<form id="form1" runat="server">
<object id="objMultiUpload" name="objMultiUpload" classid="CLSID:07B06095-5687-4d13-9E32-12B4259C9813" width="100%" ></object>
<asp:Button ID="btnMultiFile" runat="server" Text="Upload all" OnClick="btnMultiFile_Click" />
<input type="hidden" name="Confirmation-URL" value="<%= _redirectURL %>" />
<input type="hidden" id="PostURL" name="PostURL" />
</form>
</body>
</html>
The CLSID:07B06095-5687-4d13-9E32-12B4259C9813 is the CID of the Upload ActiveX Control.
Coding Page:
The attachment process will go through two steps
- Upload the file in Server’s Temp Directory
- Upload the files from Temp to Document Library
- Delete the temp File
** if (Request.Files.Count > 0)
{
HttpPostedFile File = null;
for (int i = 0; i < Request.Files.Count; i++)
{
File = Request.Files[i];
if (dtAttachFile == null)
{
AddMoreColumns();
}
if (File != null && File.FileName != String.Empty && File.ContentLength > 0)
{
drAttachFile = dtAttachFile.NewRow();
string _tempFolder = Environment.GetEnvironmentVariable("TEMP");
string _fileTime = DateTime.Now.ToFileTime().ToString();
string _fileName = System.IO.Path.GetFileName(File.FileName);
string _newfilePath = _fileTime + "~" + _fileName;
string _filepath = _tempFolder + "\" + _newfilePath;
File.SaveAs(_filepath);
drAttachFile["FileName"] = _fileName;
drAttachFile["FilePath"] = _filepath;
dtAttachFile.Rows.Add(drAttachFile);
}
}
SPList List = this._myTeamSite.Lists[Request.QueryString["List"]];
SPListItem newItem = List.Items.Add();
foreach (DataRow drAttachFile in dtAttachFile.Rows)
{
fileName = drAttachFile["Filename"].ToString();
strFilepath = drAttachFile["FilePath"].ToString();
StreamReader sr = new StreamReader(strFilepath);
Stream fStream = sr.BaseStream;
contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();
newItem.Attachments.Add(fileName, contents);
System.IO.File.Delete(strFilepath);
}
this._myTeamSite .AllowUnsafeUpdates = true;
newItem.Update();
this._myTeamSite .AllowUnsafeUpdates = false;
}**
** **
Download the source code:
Reference:
**http://msdn.microsoft.com/en-us/library/ms456628(v=office.12).aspx
For multiple file upload you can look at:
http://social.technet.microsoft.com/wiki/contents/articles/2947.aspx
Jayant Sharma**