Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Adriano Fabri 469 posts 1633 karma points
    Mar 07, 2015 @ 20:29
    Adriano Fabri
    0

    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?

  • mike 90 posts 258 karma points
    Mar 07, 2015 @ 20:43
    mike
    0

    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. 

  • Adriano Fabri 469 posts 1633 karma points
    Mar 07, 2015 @ 22:33
    Adriano Fabri
    0

    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);
    }
    
  • mike 90 posts 258 karma points
    Mar 08, 2015 @ 00:38
    mike
    0

    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.

     

  • Adriano Fabri 469 posts 1633 karma points
    Mar 08, 2015 @ 19:51
    Adriano Fabri
    0

    Hi Mike,
    I have a reserved area in wich I created a page to upload files.
    Here is this simple file upload form.

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        Layout = "Master.cshtml";
    }
    @section blockContent {
        <form id="upload" action="../upload-result" method="post" enctype="multipart/form-data" style="color: #000;">
            <fieldset>
                <legend>HTML File Upload</legend>
                <div>
                    <label for="fileselect">Files to upload:</label>
                    <input type="file" id="uploadFile" name="uploadFile" />
                    <button type="submit">Upload Files</button>
                </div>
            </fieldset>
        </form>
    }
    

    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.

    Thank you for your help

  • mike 90 posts 258 karma points
    Mar 09, 2015 @ 16:33
    mike
    1

    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?

  • Adriano Fabri 469 posts 1633 karma points
    Mar 10, 2015 @ 00:40
    Adriano Fabri
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft