Hi, I need to create a user file upload page to upload files into specific "media folder" (with razor or partial view). I tried to use the new MediaService but I did not succeed.
This is the code of the first version (razor). It upload file in the root of the media folder (on filesystem):
@inherits umbraco.MacroEngines.DynamicNodeContext
@using umbraco.MacroEngines;
@using umbraco.cms.businesslogic.media;
@using umbraco.BusinessLogic;
@{
Media UploadedMedia;
umbraco.BusinessLogic.User Me = umbraco.BusinessLogic.User.GetUser(0);
var fileName = "";
if (IsPost) {
var fileSavePath = "";
var uploadedFile = Request.Files[0];
fileName = Path.GetFileName(uploadedFile.FileName);
fileSavePath = Server.MapPath("~/media/" + fileName);
uploadedFile.SaveAs(fileSavePath);
FileInfo info = new FileInfo(fileSavePath);
string[] ext = fileName.Split('.');
UploadedMedia = Media.MakeNew(fileName,MediaType.GetByAlias("File"),Me,1058);
UploadedMedia.getProperty("umbracoFile").Value = "/media/" + fileName;
UploadedMedia.getProperty("umbracoBytes").Value = info.Length;
UploadedMedia.getProperty("umbracoExtension").Value = ext[1];
UploadedMedia.Save();
}
}
@FileUpload.GetHtml( initialNumberOfFiles: 1, allowMoreFilesToBeAdded: false, includeFormTag: true, uploadText: "Upload")
@if (IsPost) {
@Html.Raw("File uploaded!");
}
After I tried to use the MediaService and I managed to create the file media but not to upload file. This is the code (that only create the Media File node) (partial view):
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@if (ApplicationContext.Current != null) {
var ms = ApplicationContext.Current.Services.MediaService;
//Use the MediaService to create a new Media object (-1 is Id of root Media object, "Folder" is the MediaType)
var mediaMap = ms.CreateMedia("UploadTest", 1364, "File");
//Use the MediaService to Save the new Media object
ms.Save(mediaMap);
}
That's a lot of code for a view. You should probably create a surface controller in this circumstance.
I haven't tried to run the code yet. Can you explain what you mean by "I managed to create the file media but not to upload file"
If file was created how was it not uploaded? Can't remember off top of my head how I usually handle file streams (not sure I have done it with MVC yet) but let me know whats going on and I'll have a play with it.
And this is the "upload-result" page where there is the code to create new media.
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@if (ApplicationContext.Current != null)
{
var ms = ApplicationContext.Current.Services.MediaService;
//Use the MediaService to create a new Media object (-1 is Id of root Media object, "File" is the MediaType)
var mediaFile = ms.CreateMedia("UploadTest", 1364, "File");
//set the umbracofile property with upload file
FileStream s = new FileStream(Request.Querystring["uploadFile"], FileMode.Open);
mediaFile.SetValue("umbracoFile", Path.GetFileName(Request.Querystring["uploadFile"]), s);
//Use the MediaService to Save the new Media object
ms.Save(mediaMap);
}
When I try to upload one file, in the resul page I always receive "Error loading Partial View script (file: ~/Views/MacroPartials/Upload File.cshtml)"
If I remove the line with Filestream and the line with SetValue...the new media is created correctly without the uploaded file.
Sorry my not perfect english...I hope this time I explained better the problem.
I have never tried doing an Http post to a razor form. I am not sure how to get that to work. You are trying to pull image data from a "Request.Querystring" which wont work I think. Maybe "Request.Form" will work?
The way I would get this to work is to create a controller that takes the file/model/formcollection as a parameter and post to that controller. Do all logic in the controller for creating image and then return a view (or json if you want to handle the post with AJAX).
Any reason you need to save files in your view logic?
How to: File Upload with razor or partial view
Hi,
I need to create a user file upload page to upload files into specific "media folder" (with razor or partial view).
I tried to use the new MediaService but I did not succeed.
Anyone can show me how can I do it?
Create media method seems straight forward - https://our.umbraco.org/documentation/Reference/Management-v6/Services/MediaService
Post your controller code and the error (or unexpected behavior) you get and I'll have a look.
This is the code of the first version (razor).
It upload file in the root of the media folder (on filesystem):
After I tried to use the MediaService and I managed to create the file media but not to upload file.
This is the code (that only create the Media File node) (partial view):
That's a lot of code for a view. You should probably create a surface controller in this circumstance.
I haven't tried to run the code yet. Can you explain what you mean by "I managed to create the file media but not to upload file"
If file was created how was it not uploaded? Can't remember off top of my head how I usually handle file streams (not sure I have done it with MVC yet) but let me know whats going on and I'll have a play with it.
Hi Mike,
I have a reserved area in wich I created a page to upload files.
Here is this simple file upload form.
And this is the "upload-result" page where there is the code to create new media.
When I try to upload one file, in the resul page I always receive "Error loading Partial View script (file: ~/Views/MacroPartials/Upload File.cshtml)"
If I remove the line with Filestream and the line with SetValue...the new media is created correctly without the uploaded file.
Sorry my not perfect english...I hope this time I explained better the problem.
Thank you for your help
I have never tried doing an Http post to a razor form. I am not sure how to get that to work. You are trying to pull image data from a "Request.Querystring" which wont work I think. Maybe "Request.Form" will work?
The way I would get this to work is to create a controller that takes the file/model/formcollection as a parameter and post to that controller. Do all logic in the controller for creating image and then return a view (or json if you want to handle the post with AJAX).
Any reason you need to save files in your view logic?
I used the view to try if it was possible. But if the best way to do this is the controller, I'll use it.
You can help me how to fo it? this will be my first controller and I don't know very well what I must do.
Thanks
is working on a reply...