Wednesday, August 6, 2014

Sharepoint 2010 - File Uploading with HttpFileCollection

Upload document to sharepoint library using HttpFileCollection

Hello everybody,

Today I post this article to client can upload document from their computer, if we use FileUpload1.PostedFile.FileName method we can’t upload document. Because, that code only execute in server (server setup sharepoint). So I write this article using HttpFileCollection. See details:
 
Code inline
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UploadDocHttpFileCollectionUserControl.ascx.cs" Inherits="SharePointSample.UploadDocHttpFileCollection.UploadDocHttpFileCollectionUserControl" %>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
    Text="Upload document to sharepoint library" />

Code behind
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Web;
using System.IO;

namespace SharePointSample.UploadDocHttpFileCollection
{
    public partial class UploadDocHttpFileCollectionUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            using (SPSite spSite=new SPSite(SPContext.Current.Web.Url))
            {
                using (SPWeb spWeb= spSite.OpenWeb())
                {
                    spWeb.AllowUnsafeUpdates = true;
                    SPList spList = spWeb.Lists["Shared Documents"];
                    SPFile spFile = UploadDocument(spWeb, spList.Title);
                    SPListItem spListItem = spFile.Item;
                    spListItem["Title"] = spListItem.Name;
                    spListItem.Update();
                    spWeb.AllowUnsafeUpdates = false;
                    Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri);
                }
            }
        }

        public SPFile UploadDocument(SPWeb spWeb, string documentLibraryName)
        {
            string result = "success";
            HttpFileCollection hfc = HttpContext.Current.Request.Files;
            SPFile file = null;
            for (int i = 0; i < hfc.Count; i++)
            {
                try
                {
                    // stream the data into a new SharePoint list item
                    string file_name = Path.GetFileName(hfc[i].FileName);
                    var folder = spWeb.GetFolder(documentLibraryName);//documentLibraryName (Shared Documents)
                    var files = folder.Files;

                    byte[] file_content = new byte[Convert.ToInt32(hfc[i].ContentLength)];
                    hfc[i].InputStream.Read(file_content, 0, Convert.ToInt32(hfc[i].InputStream.Length));
                    file = files.Add(documentLibraryName + "/" + file_name, file_content, true);
                }
                catch (Exception ex)
                {
                    result = "failure " + ex.Message + " " + ex.InnerException;
                }
            }
            Response.Write(result);
            return file;
        }
    }
}

Demo

0 comments:

Post a Comment