1

I am developing a web-part (a form basically) with the following functions:

  1. Generate a SPListItem with info taken from textboxes (i.e save data in a database table).
  2. Generate a folder using the ID of generated SPListItem obtained in previous step (in a separate library).
  3. Select image with FileUpload control and save it in the generated folder (step 2).
  4. Press button Save for call Click event which contains the following code:

    /// <summary>
    /// Button Click event.
    /// </summary>
    protected void btnSaveData_Click(object sender, EventArgs e)
    {
        try
        {
            using (SPSite _site = new SPSite(SPContext.Current.Site.Url))
            {
                using (SPWeb _web = _site.OpenWeb(SPContext.Current.Web.ServerRelativeUrl))
                {
                    //Library who contains the folders (included the new folder to generate).
                    SPList listaImagenes = _web.Lists["FolderLibrary"]; //It's just an example.
                    //_folder.
                    SPFolder _folder = null;
                    #region Step N. save image in generated folder.
                    try
                    {
                        if (_folder != null)
                        {
                            if (FileUpload1.HasFile)
                            {
                                _folder.Files.Add(FileUpload1.FileName, FileUpload1.FileBytes);
                                _folder.Update();
                            }
                            else { lblMsg.Text += "<b>There's no file selected</b> <br/>"; }
                        }
                        else
                        {
                            lblMsg.Text += "La _folder es NULL. <br/>";
                        }
                        //Save changes.
                        _folder.Update();
                    }
                    catch (Exception ex) { lblMsg.Text += "Error. " + ex.Message + "<br/>"; }
                    #endregion
                }
            }
        }
        catch (Exception ex)
        {
            lblMsg.Text += "Error: " + ex.Message + " <br/> Stacktrace: " + ex.StackTrace;
        }
    }
    

The first two steps are complete, however, after pressing the button Save, the code checks if the FileUpload control has a file (i.e. FileUpload1.HasFile()) which is always false.

I already check another questions about FileUpload control for save images in SharePoint in the provided code in most of those questions the shared code doesn't show if they use Session variables or another way of keep the selected file in FileUpload control.

So my full question is:

How can keep the selected image and use it for add it in folder programmatically?

EDIT:

The Web-Part is a Sanboxed solution and the FileUpload control isn't in a UpdatePanel control.

  • Are you developing this solution as sandboxed solution or farm solution? – Pradip R. Dec 24 '14 at 6:54
  • Is your FileUpload control inside an Update Panel control? – Arsalan Adam Khatri Dec 24 '14 at 8:39
  • @Pro.. Sandboxed solution edited to my question. – Mauricio Arias Olave Dec 24 '14 at 13:23
  • @ArsalanAdamKhatri No. edited to my question – Mauricio Arias Olave Dec 24 '14 at 13:24
  • 1
    You cannot get file item operations using sandboxed solution. Once I have had the same issue. – Pradip R. Dec 24 '14 at 13:36
2

Sandbox Solutions do not support FileUpload or HtmlInputFile controls, you need to change it to a Farm Solution

Reference

SharePoint 2010 Sandboxed Web Parts does not support file uploading

But have a look at following question

file upload in sharepoint sandboxed solution

| improve this answer | |
1

Unfortunately, there is no support of FileUpload in SharePoint sandboxed solution. It's the limitation.

If you want to achieve the same you need to do compromise with browser compatibility by following below links:

Uploading attachements to SharePoint link using SPService

Attachment Upload Using SPServices

Though these links are for attaching file to the list item. So as per my thought we can handle how to add files to folder in SharePoint library with the help of FileReader API.

I have raised similar question in SharePoint community as well, but I got no response so I had to compromise with browser compatibility.

Hope this helps!

| improve this answer | |

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.