Upload files to Media Library through API

Faris Rjoub asked on June 16, 2017 15:00

Hi ,

I have a new Media Library created for some specific users in orders to upload some files directly through admin UI page to only specific folder , after that I will add the file using Media Library API code.

The UI page have : <cms:UIForm runat="server" ID="EditForm" ObjectType="media.file" AlternativeFormName="MediaForm1"

The alternative form have only three columns : File Name , Description and FilePath.

I changed FilePath Form Control to : Mediafile Uploader to let user upload the file. I tried to catch filepath value inside : EditForm_OnBeforeSave method , the value is empty !!?

I need to know what I missed in this case.

And is there any other way to do that.

Thanks in advanced

Correct Answer

Trevor Fayas answered on June 19, 2017 15:51

Taking a look... for a user facing form i don't see much in the form controls.

There are form control "Direct uploader"/"File Upload" that is for a File (Binary) column type that actually uploads the file content into a binary field in the database (it's an option, you can use Kentico's API then to get the file data and put it in the media library, may need to also capture the file name/type).

I think you're best bet is to just create a quick Custom Form Control using the basic FileUpload control in the .net library.

http://asp.net-tutorials.com/controls/file-upload-control/

You can also try to use This marketplace submission and just upgrade the code to 10's api.

0 votesVote for this answer Unmark Correct answer

Recent Answers


Trevor Fayas answered on June 16, 2017 15:49

The Mediafile uploader usually is to specifically upload the file. I'm trying to think if you just capture their local path, in the response could you pull from it, and i have a feeling you can't.

There may be a better answer, but one solution would be to allow them to upload a file to a temporary folder on the site, using the media uploader or similar tools, and then use the OnAfterSave instead of before save to capture the file's information, and use the API to put it in the correct location and delete the old.

My guess why the value is empty on the OnBeforeSave is because during the save (not before) it probably does the file upload, then returns the local site path.

Hope that helps some!

0 votesVote for this answer Mark as a Correct answer

Faris Rjoub answered on June 16, 2017 17:02

Yes, I couldn't get the local file path. Since its important information needed for saving the file using API.

But for now, I will go with your idea to store the file somewhere then save it using afterSave to the target directory inside media library.

Thanks Trevor.

0 votesVote for this answer Mark as a Correct answer

Faris Rjoub answered on June 18, 2017 17:35 (last edited on June 18, 2017 18:00)

Trevor, again , I tried with new custom class which have some field to hold file information. I used the form control : Metafile Uplaoder. But still I can't capture the file path or file name.

I first need to upload files from my admin UI Form to any temporary folder on the site.

Is there any control available help me for this point exactly. Thanks

0 votesVote for this answer Mark as a Correct answer

Faris Rjoub answered on June 20, 2017 11:00

Thanks Trevor, appreciate your help.

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on June 20, 2017 15:41

Tell me if it works! If not, may want to consider using a Form instead of a Custom Table, forms can handle files automatically with the form upload, and then you can handle the upload into the media library from there.

0 votesVote for this answer Mark as a Correct answer

Faris Rjoub answered on June 25, 2017 09:52 (last edited on June 25, 2017 09:52)

Its works fine with custom asp.net form :)

Your answer helped me a lot to find proper solution for my issue. Thanks Trevor.

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on June 26, 2017 14:54

No problem, please mark which answer helped the most so others don't see this and think it still needs to be answered. Good luck Faris!

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.