As the title says, I'm trying to upload a file to somewhere in my Sitecore tree from the web and I want to keep the file's extension in the DisplayName of the item.
I tried using MediaCreator, but it requires a specific filepath which I do not know when I let people select a file using asp:FileUpload. So that's no good.
I'm now using the asp:FileUpload in combination with a pipeline. When I use Sitecore.Pipelines.Upload.Save from Sitecore.Kernel it works except it removes the extension of the file from the DisplayName. So I created a custom pipeline method that looks like this:
using System;
using System.IO;
using Sitecore.Diagnostics;
using Sitecore.IO;
namespace Sitecore.Pipelines.Upload
{
public class CustomSave
{
public void Process(UploadArgs args)
{
foreach (string key in args.Files)
{
var file = args.Files[key];
if (file != null && file.FileName.Length > 0 && file.ContentLength > 0)
{
var filename = FileUtil.MakePath(args.Folder, Path.GetFileName(file.FileName), '/');
try
{
if (!args.Overwrite)
{
filename = FileUtil.GetUniqueFilename(filename);
}
file.SaveAs(filename);
EventDispatcher.DispatchTrace("File has been uploaded: " + filename);
}
catch (Exception ex)
{
Log.Error("Could not save posted file: " + filename, ex, this);
}
}
}
}
}
}
The problem I now face is that it's trying to save to my C:\ disk instead of the Sitecore database.
Any suggestions on how to write to the correct destination?
The end goal, as explained, is to use the client friendly asp:FileUpload to select a file and upload it to a specific path in Sitecore retaining the file's extension in its DisplayName. If you have another way to do that I'd like to hear about it too.
There is a setting that appends file extension to the item name "Media.IncludeExtensionsInItemNames". If you can live with that, just turn it on.
Otherwise, I'd add a custom processor into "uiUpload" after "Save" one. At that moment you would already have a list of created media items from "args.UploadedItems". You can do your simple manipulations with items in there.