Wednesday, 28 March 2012

Upload Files into sharepoint library using FileUpload control

string documentlibraryname = ConfigurationManager.AppSettings["DocumentLibraryName"];
string folderpath = ConfigurationManager.AppSettings["TempFolderLocation"];

HttpFileCollection hfc = Request.Files;

for (int i = 0; i < hfc.Count; i++)
HttpPostedFile hpf = hfc[i];
if (hpf.ContentLength > 0)
//folderpath +
//Get proper file name
string strFileName = hpf.FileName;
strFileName = Path.GetFileName(strFileName);

string TempFileName = folderpath + strFileName;
String fileToUpload = TempFileName;
String sharePointSite = SPContext.Current.Web.Url;
using (SPSite oSite = new SPSite(sharePointSite))
using (SPWeb oWeb = oSite.OpenWeb())
oWeb.AllowUnsafeUpdates = true;
if (!System.IO.File.Exists(fileToUpload))
throw new FileNotFoundException("File not found.", fileToUpload);
string[] folders = folderName.Split(';');
String url = oWeb.Lists[documentlibraryname].RootFolder.ServerRelativeUrl.ToString();
foreach (string subFolder in folders)
url += "/" + subFolder.Trim();
Boolean replaceExistingFiles = true;
String fileName = System.IO.Path.GetFileName(fileToUpload);
FileStream fileStream = File.OpenRead(fileToUpload);
SPFile spfile = oWeb.GetFolder(url).Files.Add(fileName, fileStream, replaceExistingFiles);
spfile.Item["Name"] = fileName;
spfile.Item["Created By"] = SPContext.Current.Web.CurrentUser;
spfile.Item["Modified By"] = SPContext.Current.Web.CurrentUser;
oWeb.AllowUnsafeUpdates = false;

catch (Exception ex)
lblMessage.Text = ex.ToString();
HttpFileCollection hfc1 = Request.Files;
lblMessage.Text = "Please select at least one file to upload";

Download Files from Web[c#]

Download Files from Web [C#]

This example shows how to download files from any website to local disk. The simply way how to download file is to use WebClient class and its method DownloadFile. This method has two parameters, first is the url of the file you want to download and the second parameter is path to local disk to which you want to save the file.

Download File Synchronously

The following code shows how to download file synchronously. This method blocks the main thread until the file is downloaded or an error occur (in this case the WebException is thrown).
using System.Net;

WebClient webClient = new WebClient();
webClient.DownloadFile("", @"c:\myfile.txt");

Download File Asynchronously

To download file without blocking the main thread use asynchronous method DownloadFileA­sync. You can also set event handlers to show progress and to detect that the file is downloaded.
private void btnDownload_Click(object sender, EventArgs e)
  WebClient webClient = new WebClient();
  webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
  webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
  webClient.DownloadFileAsync(new Uri(""), @"c:\myfile.txt");

private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
  progressBar.Value = e.ProgressPercentage;

private void Completed(object sender, AsyncCompletedEventArgs e)
  MessageBox.Show("Download completed!");

Note: Although you use asynchronous method, it can block the main thread for a while. It's because before the async download itself, it checks the DNS name (in this case „“) and this check is done internally by blocking function. If you use directly IP instead of domain name, the DownloadFileAsync method will be fully asynchronous.