1

Is it possible to upload a file to SharePoint using a simple HTML form POST and if so, where does the file end up?

<form action="..." method="post" enctype="multipart/form-data">
    <input type="file" name="myUploadInput" />
    ...
</form>
3

If you mean to upload file from local folder to document library than follow this method

  1. Create an application page in Microsoft Visual Studio 2010, , as described in Creating Application Pages for SharePoint.
  2. Add an HtmlInputFile control, a text box, a button to the form, and an FormDigest control to the page.

     <form id="Form1" method="post" runat="server">
       <SharePoint:FormDigest runat="server" />
       <input id="File1" type="file" runat="server" title="upldFileBox">
       <asp:Button id="Button1" runat="server" Text="Upload File"></asp:Button>
       <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
     </form>
    
  3. In the .aspx.cs code-behind file, add using directives for the System.IO and Microsoft.SharePoint namespaces, as follows.

     using System.IO;
     using Microsoft.SharePoint;
    
  4. Add the following code to the Click event for the button.

    if (File1.PostedFile == null)
      return;
    
    string destUrl = TextBox1.Text;
    
    SPWeb site = new SPSite(destUrl).OpenWeb();
    
    Stream fStream = File1.PostedFile.InputStream;
    byte[] contents = new byte[fStream.Length];
    
    fStream.Read(contents, 0, (int)fStream.Length);
    fStream.Close(); 
    
    EnsureParentFolder(site, destUrl);
    
    site.Files.Add(destUrl, contents);
    

    The value that you type in the text box for the destination must be an absolute URL, including the file name, which is assigned to the destUrl parameter. In addition to instantiating an SPWeb object for the parent site, the combined use of the SPSite constructor and OpenWeb method validates the URL and throws an argument exception if the URL is not served by the current SharePoint Foundation deployment. A HtmlInputFile object is used to read the source file into a byte array for use with the Add method of the SPFileCollection class.

  5. The example defines an EnsureParentFolder method to ensure that the parent folder in the destination URL exists in the specified site, and it returns the site-relative URL of the parent folder. The EnsureParentFolder method accepts two parameters: an SPWeb object that represents the parent site, and a string that contains the absolute URL that is passed from the example’s UploadFile method. If the parent folder does not exist, the EnsureParentFolder method creates it.

    public string EnsureParentFolder(SPWeb parentSite, string destinUrl)
    {
     destinUrl = parentSite.GetFile(destinUrl).Url;
    
     int index = destinUrl.LastIndexOf("/");
     string parentFolderUrl = string.Empty;
    
     if (index > -1)
    {
    parentFolderUrl = destinUrl.Substring(0, index);
    
    SPFolder parentFolder 
        = parentSite.GetFolder(parentFolderUrl);
    
    if (! parentFolder.Exists)
    {
        SPFolder currentFolder = parentSite.RootFolder;
    
        foreach(string folder in parentFolderUrl.Split('/'))
        {
            currentFolder 
                = currentFolder.SubFolders.Add(folder);
        }
    }
    }
      return parentFolderUrl;
    }
    
  6. The GetFile method of the SPWeb class is used in combination with the Url property of the SPFile class to convert the URL to a site-relative URL, throwing an exception if the specified URL is not found within the scope of the site. The URL of the parent folder is calculated by using the String.LastIndexOf method to determine the last appearance of a forward slash (/) within the destination URL. If there is no slash (in other words, the index equals -1), then the destination is the root folder for the site and the parentFolderUrl parameter returns an empty string. Otherwise, the example uses the GetFolder method of the SPWeb class to return the destination parent folder. If the folder does not exist, the example constructs the folder. To upload a file from a local folder that is located on the same server that is running SharePoint Foundation, you can use a FileStream object instead. In this case, add a using directive for the System.IO namespace, in addition to directives for System and Microsoft.SharePoint. The following example uses the Click event handler to call a custom UploadFile method, which in turn calls the previously described EnsureParentFolder method.

     public void UploadFile(string srcUrl, string destUrl)
    {
     if (! File.Exists(srcUrl))
    {
      throw new ArgumentException(String.Format("{0} does not exist", srcUrl), srcUrl");
     }
    
      SPWeb site = new SPSite(destUrl).OpenWeb();
    
      FileStream fStream = File.OpenRead(srcUrl);
      byte[] contents = new byte[fStream.Length];
      fStream.Read(contents, 0, (int)fStream.Length);
      fStream.Close(); 
    
      EnsureParentFolder(site, destUrl);
      site.Files.Add(destUrl, contents);
     }
    

The UploadFile method accepts two parameters. The srcUrl parameter specifies the path of the source location in the file system of the local computer, and the destUrl parameter specifies the absolute URL of the destination. A FileStream object is used to read the source file into a byte array for use with the Add method of the SPFileCollection class.

Courtesy : http://msdn.microsoft.com/en-us/library/office/ms454491(v=office.14).aspx

1

Yes, you can upload files to SharePoint. It's built in to the system, and the file you're uploading will reside in the document library you uploaded to.

enter image description here

There is no need to develop a custom control for file uploading.

  • This does not answer my question – Daniel Dec 14 '13 at 19:00
  • @Daniel from what I can read in your question, it answers the question very well. SharePoint is built on ASP.NET framework, which uses HTML technologies such as FORM action POST submission. In your questions current form I can't read that you want to make a custom uploading function. Nor can I read why you can't use out of the box funtionality. Thus I want to point you, and other, to the standard functionallity of SharePoint. – Benny Skogberg Dec 14 '13 at 20:54
  • Your answer doesn't tell me whether it's possible to upload an image using an HTML form POST. Which is what my question is. It's pretty obvious that this can be done using the OOTB UI, but that's not the question. – Daniel Dec 14 '13 at 22:58
0

If you want to upload file using your own code but completely client side, follow this tutorial on MSDN:

http://msdn.microsoft.com/en-us/library/office/jj163201.aspx#BasicOps_FileTasks

function createFile(resultpanel) {
    var clientContext;
    var oWebsite;
    var oList;
    var fileCreateInfo;
    var fileContent;

    clientContext = new SP.ClientContext.get_current();
    oWebsite = clientContext.get_web();
    oList = oWebsite.get_lists().getByTitle("Shared Documents");

    fileCreateInfo = new SP.FileCreationInformation();
    fileCreateInfo.set_url("my new file.txt");
    fileCreateInfo.set_content(new SP.Base64EncodedByteArray());
    fileContent = "The content of my new file";

    for (var i = 0; i < fileContent.length; i++) {

        fileCreateInfo.get_content().append(fileContent.charCodeAt(i));
    }

    this.newFile = oList.get_rootFolder().get_files().add(fileCreateInfo);

    clientContext.load(this.newFile);
    clientContext.executeQueryAsync(
        Function.createDelegate(this, successHandler),
        Function.createDelegate(this, errorHandler)
    );

    function successHandler() {
        resultpanel.innerHTML =
            "Go to the " +
            "<a href='../Lists/Shared Documents'>document library</a> " +
            "to see your new file.";
    }

    function errorHandler() {
        resultpanel.innerHTML = "Request failed: " + arguments[1].get_message();
    }
}
  • James thanks for posting this... very helpful to know this can be done client side and to see an example... and with the help of HTML5 File API (as browsers move fowrad to fully supported ( ((cough)) IE), getting files from the user's computer and uploading them to SP will be possible. – Paul T. Dec 14 '13 at 18:04
0

Daniel, I had this need a few years ago with a SP (2007) app I was developing and created a Widget that does it... It is meant to be used with an app that is hosted on a SharePoint site (not cross domain), and supports only uploading files to document libraries... I used it from a customized aspx page hosted inside a Document Library.

Its basically a wrapper around Sharepoint's Upload.aspx page which does some screen scrapping and manipulation in the background while only displaying a minimalist UI to the user.

Documentation here: http://purtuga.github.io/SPWidgets/ (look at the SPControlUpload widget).

Video of it in action: http://youtu.be/spOZsEtlUsQ

Hope you find a solution to your use case.

Paul.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.