0
votes

I'll explain the problem right away, but first of all...is this achievable?

I have a Document Type in Umbraco where I store data from a Form. I can store everything except the file.

        ...
        content.SetValue("notes", item.Notes);
        content.SetValue("curriculum", item.Curriculum); /*this is the file*/
        ...

I'm adding items like this where SetValue comes from the following namespace namespace Umbraco.Core.Models and this is the function signature void SetValue(string propertyTypeAlias, object value)

And the return error is the following

"String or binary data would be truncated. ↵The statement has been terminated."

Did I missunderstood something? Shouldn't I be sending the base64? I'm adding the image to a media file where it creates a sub-folder with a sequential number. If I try to add an existing folder it appends the file just fine but if I point to a new media sub-folder it also returns an error. Any ideas on how should I approach this?

Thanks in advance

Edit 1: After Cryothic answer I've updated my code with the following

        byte[] tempByte = Convert.FromBase64String(item.Curriculum);
        var mediaFile = _mediaService.CreateMedia(item.cvExtension, -1, Constants.Conventions.MediaTypes.File);
        Stream fileStream = new MemoryStream(tempByte);
        var fileName = Path.GetFileNameWithoutExtension(item.cvExtension);
        mediaFile.SetValue("umbracoFile", fileName, fileStream);
        _mediaService.Save(mediaFile);

and the error happens at mediaFile.SetValue(...).

If I upload a file from umbraco it goes to "http://localhost:3295/media/1679/test.txt" and the next one would go to "http://localhost:3295/media/1680/test.txt". Where do I tell on my request that it has to add to the /media folder and increment? Do I only point to the media folder and umbaco handles the incrementation part?

If I change on SetValue to the following mediaFile.SetValue("curriculum", fileName, fileStream); the request succeeds but the file is not added to the content itself and the file is added to "http://localhost:3295/umbraco/media" instead of "http://localhost:3295/media".

If I try the following - content.SetValue("curriculum", item.cvExtension); - the file is added to the content but with the path "http://localhost:3295/umbraco/test.txt".

I'm not understanding very well how umbraco inserts files into the media folder (outside umbraco) and how you add the media service path to the content service.

1

1 Answers

1
votes

Do you need to save base64? I have done something like that, but using the MediaService.

My project had the option to upload multiple images on mulitple wizard-steps, and I needed to save them all at once. So I looped through the uploaded files (HttpFileCollection) per step. acceptedFiletypes is a string-list with the mimetypes I'd allow.

for (int i = 0; i < files.Count; i++) {
    byte[] fileData = null;
    UploadedFile uf = null;
    try {
        if (acceptedFiletypes.Contains(files[i].ContentType)) {
            using (var binaryReader = new BinaryReader(files[i].InputStream)) {
                fileData = binaryReader.ReadBytes(files[i].ContentLength);
            }
            if (fileData.Length > 0) {
                uf = new UploadedFile {
                    FileName = files[i].FileName,
                    FileType = fileType,
                    FileData = fileData
                };
            }
        }
    }
    catch { }
    if (uf != null) {
        projectData.UploadedFiles.Add(uf);
    }
}

After the last step, I would loop throug my projectData.UploadedFiles and do the following.

var service = Umbraco.Core.ApplicationContext.Current.Services.MediaService;


var mediaTypeAlias = "Image";
var mediaItem = service.CreateMedia(fileName, parentFolderID, mediaTypeAlias);
Stream fileStream = new MemoryStream(file.FileData);
mediaItem.SetValue("umbracoFile", fileName, fileStream);
service.Save(mediaItem);

I also had a check which would see if the uploaded filename was ending on ".pdf". In that case I'd change the mediaTypeAlias to "File".

I hope this helps.