Return to doc.sitecore.com

Valid for Sitecore 5.3
Can extranet users upload media to the database?

Sitecore version: tested with 5.3.0.

Q:

Can extranet users upload media to the database?

A:

No problem with that!  Just follow the steps below to accomplish this:

  1. Add a new pipeline to the <processors> section in the web.config: 

    <frontendUpload>
    <processor mode="on" type="Sitecore.Pipelines.Upload.Save, Sitecore.Kernel" />
    </frontendUpload>

  2. Add the FileUpload control to your layout (web form):

    <asp:FileUpload ID="FileUpload1" runat="server" />

    This control will be used to select the image for uploading.

  3. In the code behind of the layout you should add the following code for example in the button click handler.

1.  Code Example

// defining if a file is being uploaded
if (FileUpload1.PostedFile != null)
{
// disabling security in order to create a media item
using (new Sitecore.SecurityModel.SecurityDisabler())
{
// creating necessary arguments to be passed to the processor
UploadArgs args = new UploadArgs();
// adding http files collection
args.Files = base.Request.Files;
// a media path where the media item will be created
args.Folder = "/sitecore/media library/Files";
// we may want to override existing media items
args.Overwrite = true;
// we do not need to choose this option since we are uploading images, not archives
args.Unpack = false;
// turning on versioning for the uploaded item
args.Versioned = true;
// selecting a language in which the media item will be created
args.Language = Sitecore.Globalization.Language.Parse("en");
// we are uploading to the database
args.Destination = UploadDestination.Database;
// if we are uploading to the master database, we need to change the active site
Sitecore.Context.SetActiveSite("shell");
// starting the pipeline previously added to web.config
PipelineFactory.GetPipeline("frontendUpload").Start(args);
// the media item is created. Now we can do whatever we want with the uploaded items
// for exampe, programmatically populate the Alt fields
foreach (Item itm in args.UploadedItems)
{
itm.Editing.BeginEdit();
itm.Fields[
"Alt"].Value = "Set from API";
itm.Editing.EndEdit();
}
// setting the active site back
Sitecore.Context.SetActiveSite("website");
}
}
else
{
Response.Write(
"No file is posted.");
}