Copied to clipboard

Flag this post as spam?

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


  • Gogo Dev 30 posts 187 karma points
    Sep 24, 2020 @ 13:20
    Gogo Dev
    0

    How to change content of existing media? CSV-File

    So I want to directly append/write things into an existing csv-file under medias only (not first creating one locally etc...), without having to use this method: HttpContext.Current.Server.MapPath(@"~\files\" + media.Name + ".csv");

    I've tried this, but of course some errors appeared, as it can't write into Uri paths or something:

    var media = Umbraco.TypedMedia(einladung.SelectedCsvMedia);
                    if (media != null)
                    {
    
                        var path = Request.Url.GetLeftPart(UriPartial.Authority) + media.Url;
                        var data = Environment.NewLine + "\"" + Request["email"] + "\";\"" + Request["surname"] + "\";\"" + Request["name"] + "\";\""+ Request["inputTextArea"]+"\";\""+registration+"\"";
                        var header = "\"email\";\"firstname\";\"lastname\";\"comment\";\"register\"";
                        IMediaService mediaService = ApplicationContext.Current.Services.MediaService;
                        var mediaFile = ApplicationContext.Current.Services.MediaService.GetById(media.Id);
                        if (media.GetProperty("umbracoBytes") == null || media.GetPropertyValue<int>("umbracoBytes") <= 5)
                        {
                            System.IO.File.WriteAllText(path, header);
    
                        }
                        System.IO.File.AppendAllText(path, data);
    
                        mediaService.Save(mediaFile);
                    }
    

    Then I tried this, but it isn't a valid virtual path:

     var media = Umbraco.TypedMedia(einladung.SelectedCsvMedia);
                    if (media != null)
                    {
    
                        var path = HttpContext.Current.Server.MapPath(Request.Url.GetLeftPart(UriPartial.Authority) + media.Url);
                        var data = Environment.NewLine + "\"" + Request["email"] + "\";\"" + Request["surname"] + "\";\"" + Request["name"] + "\";\""+ Request["inputTextArea"]+"\";\""+registration+"\"";
                        var header = "\"email\";\"firstname\";\"lastname\";\"comment\";\"register\"";
                        IMediaService mediaService = ApplicationContext.Current.Services.MediaService;
                        var mediaFile = ApplicationContext.Current.Services.MediaService.GetById(media.Id);
                        if (media.GetProperty("umbracoBytes") == null || media.GetPropertyValue<int>("umbracoBytes") <= 5)
                        {
                            System.IO.File.WriteAllText(path, header);
    
                        }
                        System.IO.File.AppendAllText(path, data);
    
                        mediaService.Save(mediaFile);
                    }
    

    Is there another way to write into an existing media file?

  • Gogo Dev 30 posts 187 karma points
    Sep 25, 2020 @ 08:59
    Gogo Dev
    100

    Ok I figured it out myself:

    var media = Umbraco.TypedMedia(einladung.SelectedCsvMedia);
                    if (media != null)
                    {
    
                        var path = Request.Url.GetLeftPart(UriPartial.Authority) + media.Url;
                        var data = Environment.NewLine + "\"" + Request["email"] + "\";\"" + Request["surname"] + "\";\"" + Request["name"] + "\";\""+ Request["inputTextArea"]+"\";\""+registration+"\"";
                        var header = "\"email\";\"firstname\";\"lastname\";\"comment\";\"register\"";
                        IMediaService mediaService = ApplicationContext.Current.Services.MediaService;
                        var mediaFile = ApplicationContext.Current.Services.MediaService.GetById(media.Id);
                        string contentToWrite = "";
                        if (media.GetProperty("umbracoBytes") == null || media.GetPropertyValue<int>("umbracoBytes") <= 5)
                        {
                            contentToWrite = header;
                        }
    
                        WebClient wb = new WebClient();
                        Stream stream = wb.OpenRead(path);
                        StreamReader sr = new StreamReader(stream);
                        contentToWrite += sr.ReadToEnd();
                        stream.Close();
                        contentToWrite += data;
    
    
                        byte[] buffer = Encoding.ASCII.GetBytes(contentToWrite);
                        MemoryStream strm = new MemoryStream(buffer);
    
                        mediaFile.SetValue("umbracoFile", media.UrlName + ".csv", strm);
    
                        mediaService.Save(mediaFile);
                    }
    
Please Sign in or register to post replies

Write your reply to:

Draft