I'm wondering if one of you talented devs is able to answer my question.
Can someone tell me how you upload a file to the media library, I've looked at the MediaService and while you can create Media, I can't see how you upload a file to go with the MediaType.
I've got a file uploader on the frontend of my website and I need to upload and add custom information like Title, Description, Category to the file.
I was tried that code. But it shows compilation error.. I dont know what is the problem..
I need to resize image with ImageResizer and Upload it into media library
Now I am directly upload Image file from file uploader to media library.
I am create a new folder inside media folder using media ID.
public string Createmedia_OnMediaFolder(HttpPostedFileBase _file)
{
if (_file != null)
{
//Declare a new dictionary to store the parameters for the image versions.
var versions = new Dictionary<string, string>();
//Define the versions to generate
versions.Add("_small", "maxwidth=600&maxheight=600&format=jpg");
//Generate each version
foreach (var suffix in versions.Keys)
{
///Create a unique file name for each files to be uploaded into media library
string File_Name = DateTime.Now.ToString("dd MMM yyyy");
string Time = DateTime.Now.ToLongTimeString();
File_Name = File_Name + " " + Time;
File_Name = File_Name.Replace(":", "_");
File_Name = File_Name.Replace(" ", "_");
Media newMedia;
umbraco.BusinessLogic.User admUser = umbraco.BusinessLogic.User.GetUser(0);
ParentMediaName = "Review";
int Parent_Id = uQuery.GetMediaByName(ParentMediaName).FirstOrDefault().Id;
newMedia = Media.MakeNew(File_Name, MediaType.GetByAlias("Image"), admUser, Parent_Id);
string newName = File_Name; // This is calcualted earlier when file is uploaded
string newFolder = Server.MapPath("/media") + "\\" + newMedia.Id;
string newFile = newFolder + "\\" + newName;
///
///Create folder for Image in media folder using MediaID.
///
///
Directory.CreateDirectory(newFolder);
_file.InputStream.Seek(0, SeekOrigin.Begin);
//Let the image builder add the correct extension based on the output file type
ImageBuilder.Current.Build(
new ImageJob(
_file.InputStream,
newFile,
new Instructions(versions[suffix]),
false,
true));
FileInfo info = new FileInfo(newFile+".jpg");
///
///Create Image inside the folder to be created using media ID.
///
newMedia.getProperty("umbracoFile").Value = newFile+".jpg";
newMedia.getProperty("umbracoBytes").Value = info.Length;
newMedia.getProperty("umbracoExtension").Value = "jpg";
int created_media_Id = newMedia.Id;
newMedia.Save();
return created_media_Id.ToString();
}
}
return "";
}
You can alter the styles, by creating your form in the back office and clicking on the Actions button in the top right hand corner and select the Styling option. From here you can disable the built in stylesheet and use your own.
Alternatively, if that doesn't work you can take a look in the following folder:-
/Views/Partials/Forms/Fieldtypes
In here, you'll find the templates for each form element.
Addressing the compilation error Abdul was having when passing in a FileStream - you need to add a using statement for Umbraco.Core.Models to get the SetValue() overload that takes a FileStream.
Uploading a file to the Media Library via Code
Hello,
I'm wondering if one of you talented devs is able to answer my question.
Can someone tell me how you upload a file to the media library, I've looked at the MediaService and while you can create Media, I can't see how you upload a file to go with the MediaType.
I've got a file uploader on the frontend of my website and I need to upload and add custom information like Title, Description, Category to the file.
Any examples would be greatly appreaciated
Many Thanks
Paul
Hi Paul,
The Mediaservice is the way to go. Use a FileStream to read the file and use .SetValue method to store the file
When you save the media it should have the file stored.
Hope this helps,
Richard
Thanks Richard, I'll give it a try.
Just to confirm Richards suggestion worked a treat. Many Thanks
Here's my code if anyone needs it - I used it in a custom workflow for contour.
Hi Paul,
Thanks for this post...
I got an error when I using the above code in my app.
IMediaService ms = Services.MediaService;
string path_ = Server.MapPath("~/Assets/rahim.jpg");
using (var fs = System.IO.File.OpenRead(path_))
{
var mediaFile = ms.CreateMedia("rahim", uQuery.GetMediaByName(ParentMediaName).FirstOrDefault().Id, "Image",0);
mediaFile.SetValue("umbracoFile", fs);
ms.Save(mediaFile);
}
this is my error
The best overloaded method match for 'Umbraco.Core.Models.ContentBase.SetPropertyValue(string, string)' has some invalid arguments
on this line
mediaFile.SetValue("umbracoFile", fs);
please help me
Hi Abdul,
The only difference I noticed in the code was that in mine I had:-
mediaFile.SetValue("umbracoFile", originalFileName, fs);
where as in yours it was:-
mediaFile.SetValue("umbracoFile", fs);
Try setting the new filename, when you're saving the file.
Hope this helps.
Cheers
Paul
Hi paul ,
I was tried that code. But it shows compilation error.. I dont know what is the problem..
I need to resize image with ImageResizer and Upload it into media library
Now I am directly upload Image file from file uploader to media library.
I am create a new folder inside media folder using media ID.
Now it is working perfectly I want..
Thanks for your response.
Hi paul.
Now I'm working on umbraco 7.2 forms.
I dont know how to apply styles for forms. I need to apply styles for each controls. I have more than one forms with diffrerent styles .
I googled lot . But I didn't get a correct solution...
Please help me.
Hi Abdul,
You can alter the styles, by creating your form in the back office and clicking on the Actions button in the top right hand corner and select the Styling option. From here you can disable the built in stylesheet and use your own.
Alternatively, if that doesn't work you can take a look in the following folder:-
/Views/Partials/Forms/Fieldtypes
In here, you'll find the templates for each form element.
Hope this helps.
Cheers
Paul
Thanks buddy
Addressing the compilation error Abdul was having when passing in a FileStream - you need to add a using statement for Umbraco.Core.Models to get the SetValue() overload that takes a FileStream.
Dallas
is working on a reply...