using MyCorp.MyLib.Entities.WebService; using Sitecore.Configuration; using Sitecore.Resources.Media; using Sitecore.SecurityModel; using Sitecore.Services.Infrastructure.Web.Http; using System; using System.IO; using System.Linq; using System.Net; using System.Net.Http; using System.Threading.Tasks; using System.Web; using System.Web.Http; namespace MyCorp.MyLib.Feature.EmailService { [OverrideExceptionFilters] public class ImageServiceController : ServicesApiController { /// <summary> /// Save stream as file into sitecore /// </summary> /// <param name="stream"></param> /// <param name="req"></param> private void SaveFileIntoSitecore(Stream stream, ImageRequest req ) { var mediaCreator = new MediaCreator(); var options = new MediaCreatorOptions { AlternateText = req.AltText, OverwriteExisting = true, FileBased = false, Versioned = false, IncludeExtensionInItemName = false, Database = Factory.GetDatabase("master"),//web? Destination = req.DestinationName ///logo.jpg }; using (var memoryStream = new MemoryStream()) { stream.CopyTo(memoryStream); using (new SecurityDisabler()) mediaCreator.CreateFromStream(memoryStream, req.DestinationName, options); } } [HttpPost, OverrideExceptionFilters] public async Task<HttpResponseMessage> Upload() { if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } string root = HttpContext.Current.Server.MapPath("~/App_Data"); var provider = new MultipartFormDataStreamProvider(root); try { // Read the form data and return an async data. var result = await Request.Content.ReadAsMultipartAsync(provider); ImageRequest req = new ImageRequest(); // This illustrates how to get the form data. foreach (var key in provider.FormData.AllKeys) { foreach (var val in provider.FormData.GetValues(key)) { // return multiple value from FormData if (key == "DestinationName") { req.DestinationName = val; } else if (key == "DestinationFolder") { req.DestinationFolder = val; } } } if (result.FileData.Any()) { // This illustrates how to get the file names for uploaded files. foreach (var file in result.FileData) { FileInfo fileInfo = new FileInfo(file.LocalFileName); if (fileInfo.Exists) { //Save every file to sitecore var s = fileInfo.OpenRead(); SaveFileIntoSitecore(s, req); } } } HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, req); return response; } catch (System.Exception e) { return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e); } } } }
Tuesday
sitecore upload image programmatically to media library
here is how controller class looks like:
Subscribe to:
Post Comments (Atom)
test smtp server with powershell
Send-MailMessage -SMTPServer smtp.domain.com -To [email protected] -From [email protected] -Subject "This is a test email" -Body ...
-
Here is instruction how to make blinking text in rainmeter: 1.right click and select " Edit skin " 2.add following code to temp...
-
Error:The element 'Schedule' has invalid child element 'RecurrenceRule'. List of possible elements expected: 'Occurring...
-
If you use Visual Studio to open a Web project that was developed for the .NET Framework 2.0, the .NET Framework 3.0, or the .NET Framework...
No comments:
Post a Comment