The .SetValue method throws an exception, what am I doing wrong?
{"Message":"An error has occurred.","ExceptionMessage":"Parameter is not valid.","ExceptionType":"System.ArgumentException","StackTrace":" at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)\r\n at Umbraco.Core.Models.ContentExtensions.SetFileOnContent(IContentBase content, String propertyTypeAlias, String filename, Stream fileStream)\r\n at Umbraco.Core.Models.ContentExtensions.SetValue(IContentBase content, String propertyTypeAlias, HttpPostedFileBase value)\r\n at Umbraco.Core.Models.ContentBase.SetPropertyValue(String propertyTypeAlias, HttpPostedFile value)\r\n at CallSite.Target(Closure , CallSite , Object , String , Object )\r\n at Umbraco.Core.Models.ContentBase.SetValue(String propertyTypeAlias, Object value)\r\n at _201401_001_VVEAPPBACAZURE.Controllers.ImageApiController.PostUpload() in c:\\a\\src\\201401-001-VVEAPPBACAZURE\\201401-001-VVEAPPBACAZURE\\Controllers\\ImageApiController.cs:line 47\r\n at lambda_method(Closure , Object , Object[] )\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass13.<GetExecutor>b__c(Object instance, Object[] methodParameters)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.<>c__DisplayClass5.<ExecuteAsync>b__4()\r\n at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)"}
The only difference between Sebastiaans approach and mine is de Model.Files part. Do you know some example or gist where a mvc-controller uploads an image to umbraco v6?
My current approach is this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using Umbraco.Web.WebApi;
using Umbraco.Core.Models;
using umbraco.NodeFactory;
using umbraco;
using System.IO;
namespace _201401_001_VVEAPPBACAZURE.Controllers
{
public class ImageApiController : UmbracoApiController
{
public string GetUpload()
{
return "This is a get requst, not a post!";
}
public static List<string> AllowedFormats
{
get { return new List<string>() { ".jpg", ".png", ".jpeg" }; }
}
public HttpResponseMessage PostUpload()
{
HttpResponseMessage result = null;
var httpRequest = HttpContext.Current.Request;
if (httpRequest.Files.Count > 0)
{
IEnumerable<Node> nodes = uQuery.GetNodesByType("ApiSettings");
int uploadFolderId = Convert.ToInt32(nodes.First().GetProperty("imageUploadFolder").Value);
foreach (string file in httpRequest.Files)
{
if(AllowedFormats.Any(format => (Path.GetExtension(httpRequest.Files[file].FileName) == format))) {
if (httpRequest.Files[file] != null && httpRequest.Files[file].ContentLength > 0)
{
IMedia folder = Services.MediaService.CreateMedia(httpRequest["vve"], uploadFolderId, "Folder");
Services.MediaService.Save(folder);
IMedia subfolder = Services.MediaService.CreateMedia(DateTime.Now.ToString(), folder.Id, "Folder");
Services.MediaService.Save(subfolder);
IMedia newImage = Services.MediaService.CreateMedia(httpRequest.Files[file].FileName, subfolder.Id, "Image");
newImage.SetValue("umbracoFile", httpRequest.Files[file]);
Services.MediaService.Save(newImage);
result = Request.CreateResponse(HttpStatusCode.Created, newImage.Id.ToString());
}
else
{
result = Request.CreateResponse(HttpStatusCode.BadRequest, "File is null");
}
}
else
{
result = Request.CreateResponse(HttpStatusCode.BadRequest, "Not an image");
}
}
}
else
{
result = Request.CreateResponse(HttpStatusCode.BadRequest, "No files");
}
return result;
}
}
}
Update: when I post my request with Chrome's Plugin POSTMAN everything works fine. Somehow the stream is not send properly. So maybe the problem isn't with the controller.
"Parameter is not valid" error on using u6 Media API.
Umbraco 7 on Windows Azure.
I want to use the Media API in my Controller to handle some media uploads:
The .SetValue method throws an exception, what am I doing wrong?
Hello,
Try to compare it to this example to see if you're doing something wrong: https://gist.github.com/nul800sebastiaan/8008892
Jeroen
-- THERE'S AN UPDATE BELOW --
The only difference between Sebastiaans approach and mine is de Model.Files part. Do you know some example or gist where a mvc-controller uploads an image to umbraco v6?
My current approach is this:
Update: when I post my request with Chrome's Plugin POSTMAN everything works fine. Somehow the stream is not send properly. So maybe the problem isn't with the controller.
is working on a reply...