SharePoint - Upload a file from web part - Asked By Bookworm on 23-Jan-12 03:00 AM

Hi,

im uploading a file in a SP List from a web part, and the workflow is that the user browse a file, then upload it in a temp folder to the server and then save this file in SP list, but the problem is that not all the users have access into that temp folder on server, how can i save the file in SP list directly from the path that file is chosen?

any idea?

thnx
Jitendra Faye replied to Bookworm on 23-Jan-12 03:02 AM
Try like this-


If fuMyUpload.HasFile Then

          Dim struploadPath As String = "http://win2k8sp1:1999/MyUploads/" + fuMyUpload.FileName.ToString
          Dim oSite As New SPSite("http://win2k8sp1:1999")
          Dim oWeb As SPWeb = oSite.OpenWeb()
          Dim oUser As SPUser = oWeb.CurrentUser
          Dim strGroupName As String
          For Each oGroup As SPGroup In oUser.Groups
            strGroupName = oGroup.Name.ToString
          Next
          Dim files As SPFileCollection = oWeb.GetFolder("MyUploads").Files
          oWeb.AllowUnsafeUpdates = True



          Dim fStream As Stream = fuMyUpload.PostedFile.InputStream
          Dim contents(fStream.Length) As Byte
          fStream.Read(contents, 0, CInt(fStream.Length))
          fStream.Close()


          Dim newFile As SPFile = files.Add(struploadPath, contents, True)
          newFile.Item("Description") = txtDescription.Text
          newFile.Item("Expire On") = dtcExpireDate.SelectedDate
          newFile.Item("Client") = strGroupName
          newFile.Item.Update()

 
        End If

Riley K replied to Bookworm on 23-Jan-12 03:06 AM


Do you mean to programatically upload a file

try like this to upload file to doc library

121@in.airtel.com
 
 
private void UploadFileToSharePoint(string strInputFileName,string sDocLibraryName)
 
    {
 
      string destUrl = ConfigurationManager.AppSettings["destURL"];       
 
      SPWeb site = new SPSite(destUrl).OpenWeb();
 
       SPList myList = site.Lists[sDocLibraryName];
 
      string destFileUrl = myList.RootFolder.ServerRelativeUrl + @"/New.txt";
 
       site.AllowUnsafeUpdates = true;
      FileStream fileStream = File.Open(strInputFileName, FileMode.Open);
      site.Files.Add(destFileUrl, fileStream, true/*overwrite*/);
      fileStream.Close();
 
    }


Regards
kalpana aparnathi replied to Bookworm on 23-Jan-12 03:07 AM
hi,

Try this code:

public class UploadWP : WebPart
{
  private FileUpload fileUpload = new FileUpload();
  private Button btnUpload = new Button();
 
  private string documentLibrary;
 
  [WebBrowsable, Personalizable]
  public string DocumentLibrary
  {
    get
    {
      return documentLibrary;
    }
    set
    {
      documentLibrary = value;
    }
  }
 
  protected override void CreateChildControls()
  {
    Controls.Add(fileUpload);
    Controls.Add(btnUpload);
 
    btnUpload.Text = "Upload";
    btnUpload.Click += new EventHandler(OnUploadClick);
  }
 
  void OnUploadClick(object sender, EventArgs e)
  {
    SPList documentLibrary;
    try
    {
      documentLibrary = ValidateList();
    }
    catch (Exception ex)
    {
      Controls.Add(new LiteralControl(ex.Message));
      return;
    }
 
    Stream fStream = fileUpload.PostedFile.InputStream;
    byte[] contents = new byte[fStream.Length];
    fStream.Read(contents, 0, (int)fStream.Length);
    fStream.Close();
 
    SPSecurity.CatchAccessDeniedException = false;
    try
    {
      SPContext.Current.Web.Files.Add(documentLibrary.RootFolder.Url + "/" + fileUpload.FileName, contents);
    }
    catch (UnauthorizedAccessException)
    {
      Controls.Add(new LiteralControl("You are not authorized to upload files to the document library"));
    }
  }
 
  private SPList ValidateList()
  {
    if (string.IsNullOrEmpty(DocumentLibrary))
    {
      throw new Exception("Please fill the Document Library property!");
    }
 
    if (!fileUpload.HasFile)
    {
      throw new Exception("Please select a file to upload.");
    }
 
    try
    {
      return SPContext.Current.Web.Lists[DocumentLibrary];
    }
    catch (ArgumentException)
    {
      throw new Exception("List not found!");
    }
  }
}
Bookworm replied to kalpana aparnathi on 23-Jan-12 04:51 AM
thnx a lot kalpana!
kalpana aparnathi replied to Bookworm on 23-Jan-12 04:55 AM
your welcome!