Kentico - Pass MemoryStream file to MediaFileInfo API

谁说胖子不能爱 提交于 2021-02-10 14:20:38

问题


I have created a iTextSharp PDF file that is created to a MemoryStream. But I now need to pass this file to the Kentico media library.

I would be grateful if anyone could show my how to do this. The code I have currently is:

//Media Library Info - takes Media Library Name and Website Name
        MediaLibraryInfo libraryInfo = MediaLibraryInfoProvider.GetMediaLibraryInfo("MyLibrary", CMSContext.CurrentSiteName);

        //Folder in Media Library where Item will be Inserted
        string mediaLibraryFolder = folder;

        //create media file info item - takes the relative path to the document, the library ID, and the folder name where the document will be located within the media library
        MediaFileInfo fileInfo = new MediaFileInfo();

        fileInfo.FileLibraryID = libraryInfo.LibraryID;
        fileInfo.FileBinaryStream = file;
        fileInfo.FileName = title.Replace(" ", "").Trim();
        fileInfo.FileTitle = title;
        fileInfo.FileDescription = description;
        fileInfo.FileExtension = ".pdf";
        fileInfo.FileMimeType = "application/pdf";
        fileInfo.FilePath = String.Concat("/", folder, "/", title.Replace(" ", "").Trim(), ".pdf");

        // Save media file info
        MediaFileInfoProvider.ImportMediaFileInfo(fileInfo);

I keep getting database errors due to nullable columns e.g. FileSize, FileExtension, etc. Since I am using a MemoryStream I can't find a way to supply all that information.

Am I using the MediaFileInfo API incorrectly in conjunction with a MemoryStream file?


回答1:


Actually, I don't think that you need to do anything that RadekM said. You can simply stream the file to disk to save it, and then call the import method you're using to import it into the media library.

For example, a Media Library called "Site Images" for the site "MySite" will have a folder on disk at /MySite/media/Site Images/. Drop your file into there (you can use sub folders if you want). At this point the file is "in" the media library, but it hasn't been imported yet, so you wont be able to use it. You can see this is true by viewing the Media Library in the CMS Desk interface. However, this file has not yet been imported into the Media Library and you should see an exclamation point inside a yellow triangle next to your new file.

So after you get the file in the right location, you can use that file information to populate the MediaFileInfo object and Import the file.




回答2:


Could you adapt this code and pass the bytes of the PDF from here?

programmatically adding files to the Kentico Media Library




回答3:


Regrettably, MemoryStream class does not contain these informations, so you can’t gain them from this object directly. Anyway, if you want to supply FileSize property, you can use ms.Length property as a workaround. Basically, this particular property is not important, so it can be even some dummy number. As for extension – are you saying that you are receiving error saying this property is null, although you set it like „fileInfo.FileExtension = ".pdf";“? Can you clarify? Also please note that you need to set some other properties, FileSiteID, FileCreatedWhen, FileGUID and FilePath (path inside given media library). If you have full source code of Kentico API, you can get an inspiration from constructor of MediaFileInfo object in \MediaLibrary\MediaFileInfo.cs class.



来源:https://stackoverflow.com/questions/7350659/kentico-pass-memorystream-file-to-mediafileinfo-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!