Sitecore version: tested with 5.3.0.
Can extranet users upload media to the database?
No problem with that! Just follow the steps below to accomplish this:
<frontendUpload>
<processor mode="on" type="Sitecore.Pipelines.Upload.Save, Sitecore.Kernel" />
</frontendUpload>
<asp:FileUpload ID="FileUpload1" runat="server" />
This control will be used to select the image for uploading.
// 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.");
}