Umbraco Media API - Creating new items from existing files
I'm trying to use the media API's to create an API control that takes a known file on the file system and creates an element withing the media library for it.
My model is
public class MediaUploadModel
{
public string Filename { get; set; }
public string type { get; set; }
public int node { get; set; }
public string umbracoWidth { get; set; }
public string umbracoHeigth { get; set; }
public string umbracoExtension { get; set; }
public string umbracoBytes { get; set; }
}
The controller creates the new media item correctly but when the save method is called the setValue doesn't appear to be working correctly (I get values of -1 for the width,height and bytes the correct extension and a blank picture).
I'm currently trying to import files stored in the root folder of the site, but aim to copy the files to the media folder then put then in correct folders within the media library.
And all the POST requests will come from a non-web app (currently being simulated by a simple HTML form).
I think in the above you are setting the FileName to be the filePath, but the filePath, will probably be in the format /files/filename.doc and so therefore not be a valid FileName.
Also you are using file.Name to set the FileExtension, however the Name will be the 'name' eg filename and not 'doc'
I think I overrode the base properties of HttpPostedFileBase, eg
public override int ContentLength
{
get
{
return (int)_fileInfo.Length;
}
}
but that might not make a difference, just having the correct filename may make it work!
Umbraco Media API - Creating new items from existing files
I'm trying to use the media API's to create an API control that takes a known file on the file system and creates an element withing the media library for it.
My model is
And my Controller is
The controller creates the new media item correctly but when the save method is called the setValue doesn't appear to be working correctly (I get values of -1 for the width,height and bytes the correct extension and a blank picture).
I'm currently trying to import files stored in the root folder of the site, but aim to copy the files to the media folder then put then in correct folders within the media library.
And all the POST requests will come from a non-web app (currently being simulated by a simple HTML form).
Thanks in Advance.
Hi
I think the problem here is that the umbracoFile property is expecting a HttpPostedFile rather than a file path string.
One way to work around this would be to create a new class that inherits HttpPostedFileBase
that takes a file path as a constructor.
Then use FileInfo class to generate information about the file to allow you to override
using the FileInfo objects OpenRead() to create the InputStream from disc rather than a posted file
Because your new class inherits HttpPostedFileBase, the SetValue("umbracoFile") should be happy to accept the new file
Thanks Marc,
However I'm now getting the following exception thrown
With the following stack trace,
On the SetValue method.
My new class is
Any thoughts? Thanks again.
I think in the above you are setting the FileName to be the filePath, but the filePath, will probably be in the format /files/filename.doc and so therefore not be a valid FileName.
Also you are using file.Name to set the FileExtension, however the Name will be the 'name' eg filename and not 'doc'
I think I overrode the base properties of HttpPostedFileBase, eg
but that might not make a difference, just having the correct filename may make it work!
is working on a reply...