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>
If you mean to upload file from local folder to document library than follow this method
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>
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;
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.
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;
}
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
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.
There is no need to develop a custom control for file uploading.
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();
}
}
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.