How would I go about creating a upload form on the front end to allow users to upload files to the media library?
I have the following code:
var fileName = ""; if (IsPost) { var fileSavePath = ""; var uploadedFile = Request.Files[0]; fileName = Path.GetFileName(uploadedFile.FileName); fileSavePath = Server.MapPath("~/media/" + fileName); uploadedFile.SaveAs(fileSavePath); }
which uploads files to the media folder, although the files arn't added to the Media Library on the backend. I assume I need to use the Media properties but I'm pretty much lost.
Just a note (stumbled on this myself just now) - if you're not using includeFormTag (for example within a bigger form), you'll need to add the attribute enctype="multipart/form-data" to your form tag to get the files.
Upload Form to Media Library
Hi all,
How would I go about creating a upload form on the front end to allow users to upload files to the media library?
I have the following code:
var fileName = "";
if (IsPost) {
var fileSavePath = "";
var uploadedFile = Request.Files[0];
fileName = Path.GetFileName(uploadedFile.FileName);
fileSavePath = Server.MapPath("~/media/" +
fileName);
uploadedFile.SaveAs(fileSavePath);
}
which uploads files to the media folder, although the files arn't added to the Media Library on the backend. I assume I need to use the Media properties but I'm pretty much lost.
Any help would be much appreciated !!
Maybe have a look at this package: http://our.umbraco.org/projects/website-utilities/gecko-uploadify. It also has a usercontrol for frontend uploading.
Jeroen
Thanks Jeroen, sadly the package didn't work, although I managed to solve my problem. For anyone with a similar problem here's my code:
@using umbraco.MacroEngines;
@using umbraco.cms.businesslogic.media;
@using umbraco.BusinessLogic;
@{
Media UploadedMedia;
umbraco.BusinessLogic.User Me = umbraco.BusinessLogic.User.GetUser(0);
var fileName = "";
if (IsPost)
{
var fileSavePath = "";
var uploadedFile = Request.Files[0];
fileName = Path.GetFileName(uploadedFile.FileName);
fileSavePath = Server.MapPath("~/media/" + fileName);
uploadedFile.SaveAs(fileSavePath);
FileInfo info = new FileInfo(fileSavePath);
string[] ext = fileName.Split('.');
UploadedMedia = Media.MakeNew(fileName,MediaType.GetByAlias("File"),Me,1058);
UploadedMedia.getProperty("umbracoFile").Value = "/media/" + fileName;
UploadedMedia.getProperty("umbracoBytes").Value = info.Length;
UploadedMedia.getProperty("umbracoExtension").Value = ext[1];
UploadedMedia.Save();
}
}
@FileUpload.GetHtml(
initialNumberOfFiles: 1,
allowMoreFilesToBeAdded: false,
includeFormTag: true,
uploadText: "Upload")
@if (IsPost)
{
File uploaded!
}
Thanks for this
Just a note (stumbled on this myself just now) - if you're not using includeFormTag (for example within a bigger form), you'll need to add the attribute enctype="multipart/form-data" to your form tag to get the files.
is working on a reply...