I'm developing a webpart (a form basically) with the following functions:
- Generate a SPListItem with info taked from textboxes (i.e save data in a database table).
- Generate a folder using the "ID" of generated SPListItem obtained in previous step (in a separate library).
- Select image with FileUpload control and save it in the generated folder (step 2).
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?
0 commentaires:
Enregistrer un commentaire