Hello, i have a page with a form. I am using the contentService to create content in Umbraco. I have only 2 fields, a Header and a description field. How can i in that form also upload a file and save it on the node the contentservice are creating. I have a mediapicker property on the node i am creating through the content service. Do i have to make a upload with the mediaService first og can i do the both in one action?
I now have this in my controller:
public class ContactFormSurfaceController : SurfaceController
{
// GET: ContactFormSurface
public ActionResult Index()
{
return PartialView("ContactForm", new ContactFormViewModel());
}
[HttpPost]
public ActionResult HandleFormSubmit(ContactFormViewModel model)
{
//
if (!ModelState.IsValid)
return CurrentUmbracoPage();
//Create content
var newRecord = Services.ContentService.CreateContent(model.PostName, CurrentPage.Id, "federationRecordPage");
newRecord.SetValue("header", model.PostName);
newRecord.SetValue("text", model.Text);
Services.ContentService.SaveAndPublishWithStatus(newRecord);
TempData["success"] = true;
return RedirectToCurrentUmbracoPage();
}
}
My model:
public class ContactFormViewModel
{
[Required]
public string PostName { get; set; }
[Required]
public string Text { get; set; }
public int GalleryId { get; set; }
public IEnumerable<HttpPostedFileBase> Files { get; set; }
}
i have tried to follow the media service guide on UmbracoTv.
Add image to contentService creation.
Hello, i have a page with a form. I am using the contentService to create content in Umbraco. I have only 2 fields, a Header and a description field. How can i in that form also upload a file and save it on the node the contentservice are creating. I have a mediapicker property on the node i am creating through the content service. Do i have to make a upload with the mediaService first og can i do the both in one action?
I now have this in my controller:
My model:
i have tried to follow the media service guide on UmbracoTv.
Hello Christian, do you want to save that image in media section of Umbraco or just on disk?
This is how i do id:
// Iniitialize new item
// Upload the file to the server and Umbraco media
// Set Media picker value
Hello Josip, i want to save it in the media section of Umbraco yes.
Thanks for the reply i will try that.
Hello Josip,
I have now tried this.
I cannot use the "Services.ContentTypeBaseServices" only "Services.ContentTypeService" but it says it cannot take 4 arguments.
Can you help a little more?
Hi Cristian, now when you said that i checked and i see that you are asking for Umbraco 7 and my solution is for Umbraco 8.
I am not sure how to do it in Umbraco 7 because i worked with content service only in Umbraco 8 :(
But you have tutorial on Umbraco tv.
This is how to upload the image to the media section: https://umbraco.tv/videos/umbraco-v7/developer/fundamentals/media-api/
And if you want to set value for media picker property , you should know thay you need guid of the image to set value for media picker.
If you dont find the solution, write me back and i will try to do it in Umbraco 7.
BR
Josip
I got it to work. Solution:
is working on a reply...