My code to create an Umbraco media item is like this
string fairLogopath = "/Images/FairImages/986185826.png";
IMedia media = ApplicationContext.Current.Services.MediaService.CreateMedia("name", -1, Constants.Conventions.MediaTypes.Image);
var mediaService = Services.MediaService;
mediaService.Save(media); // called it before so it creates a media id
FileImportWrapper file = new FileImportWrapper(IOHelper.MapPath(fairLogopath));
media.SetValue(Constants.Conventions.Media.File, file);
ApplicationContext.Current.Services.MediaService.Save(media);
Everything looks okay to me, but this throws an error
The process cannot access the file 'C:\Users\Documents\Studio\media\1115\986185826_thumb.png' because it is being used by another process.
Can anyone please point out what I am doing wrong here?
Hi,
I was using the class above until I had the need to "clean" the file once uploaded and I found out that the method above leaves the input file opened.
To compensate we need to integrate the FileImportWrapper as below:
First: we declare a private FileStream
private FileStream fileStream;
Then we initialize and return that object in the InputStream property:
public override System.IO.Stream InputStream
{
get
{
fileStream = this.fileInfo.OpenRead();
return fileStream;
}
}
and we create a new property "Close" as below:
public bool Close
{
get
{
if (fileStream != null)
fileStream.Close();
return true;
}
}
When using the wrapper, after saving the media item, you just:
bool x = BinaryFile.Close;
and the file will not be in use anymore.
You can delete the source file just after the Close method.
To summarize, below the complete class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Web;
using System.Reflection;
/// <summary>
/// Class needed to save a binary file in Umbraco using the MediaService
/// </summary>
namespace BusinessObjects
{
public sealed class FileImportWrapper : HttpPostedFileBase
{
private FileInfo fileInfo;
private FileStream fileStream;
public FileImportWrapper(string filePath)
{
this.fileInfo = new FileInfo(filePath);
}
public bool Close
{
get
{
if (fileStream != null)
fileStream.Close();
return true;
}
}
public override int ContentLength
{
get
{
return (int)this.fileInfo.Length;
}
}
public override string ContentType
{
get
{
return MimeExtensionHelper.GetMimeType(this.fileInfo.Name);
}
}
public override string FileName
{
get
{
return this.fileInfo.FullName;
}
}
public override System.IO.Stream InputStream
{
get
{
fileStream = this.fileInfo.OpenRead();
return fileStream;
}
}
public static class MimeExtensionHelper
{
static object locker = new object();
static object mimeMapping;
static MethodInfo getMimeMappingMethodInfo;
static MimeExtensionHelper()
{
Type mimeMappingType = Assembly.GetAssembly(typeof(HttpRuntime)).GetType("System.Web.MimeMapping");
if (mimeMappingType == null)
throw new SystemException("Couldnt find MimeMapping type");
ConstructorInfo constructorInfo = mimeMappingType.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, Type.EmptyTypes, null);
if (constructorInfo == null)
throw new SystemException("Couldnt find default constructor for MimeMapping");
mimeMapping = constructorInfo.Invoke(null);
if (mimeMapping == null)
throw new SystemException("Couldnt find MimeMapping");
getMimeMappingMethodInfo = mimeMappingType.GetMethod("GetMimeMapping", BindingFlags.Static | BindingFlags.NonPublic);
if (getMimeMappingMethodInfo == null)
throw new SystemException("Couldnt find GetMimeMapping method");
if (getMimeMappingMethodInfo.ReturnType != typeof(string))
throw new SystemException("GetMimeMapping method has invalid return type");
if (getMimeMappingMethodInfo.GetParameters().Length != 1 && getMimeMappingMethodInfo.GetParameters()[0].ParameterType != typeof(string))
throw new SystemException("GetMimeMapping method has invalid parameters");
}
public static string GetMimeType(string filename)
{
lock (locker)
return (string)getMimeMappingMethodInfo.Invoke(mimeMapping, new object[] { filename });
}
}
}
}
and here is how I use it:
BusinessObjects.FileImportWrapper BinaryFile = new BusinessObjects.FileImportWrapper(SourceDocumentFile);
MediaDocument.SetValue(Constants.Conventions.Media.File, BinaryFile);
UmbracoMedia.Save(MediaDocument);
bool x = BinaryFile.Close;
Unable to create media item programatically
My code to create an Umbraco media item is like this
Everything looks okay to me, but this throws an error
Can anyone please point out what I am doing wrong here?
Hi Athul
Why did you call .Save method 2 times?
/Alex
removed the second save and changed my code as Dave suggested, still I am getting same error :(
Hi Athul,
Can you try this code :
Dave
Throwing same error
Hi Athul,
What is FileImportWrapper ? This doesn't seem to be a Umbraco object ? Is it something custom ?
Dave
Yes, This is the complete code of it.
If there is anyway to achieve this with out this helper. pls let me know
Hi athul,
It will be your custom class "FileImportWrapper" that will be holding onto the file that you are trying to use.
You can test out this theory quickly, by just commenting out the following line:
Let me know how it goes.
EDIT: Would also be worth showing your code for 'IOHelper.cs' so we can see what you are doing in this file.
Regards
Craig
Hi, I was using the class above until I had the need to "clean" the file once uploaded and I found out that the method above leaves the input file opened.
To compensate we need to integrate the FileImportWrapper as below:
First: we declare a private FileStream
Then we initialize and return that object in the InputStream property:
and we create a new property "Close" as below:
When using the wrapper, after saving the media item, you just:
and the file will not be in use anymore. You can delete the source file just after the Close method.
To summarize, below the complete class:
and here is how I use it:
Enjoy!
Michele
is working on a reply...