Kentico-将MemoryStream文件传递给MediaFileInfo API [英] Kentico - Pass MemoryStream file to MediaFileInfo API

查看:122
本文介绍了Kentico-将MemoryStream文件传递给MediaFileInfo API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个创建到MemoryStream的iTextSharp PDF文件.但是我现在需要将此文件传递给Kentico媒体库.

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);

由于列为空,我不断收到数据库错误,例如FileSize,FileExtension等.由于我使用的是MemoryStream,所以找不到提供所有这些信息的方法.

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.

我是否错误地将MediaFileInfo API与MemoryStream文件结合使用?

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

推荐答案

实际上,我认为您不需要执行RadekM所说的任何事情.您可以简单地将文件流式传输到磁盘以保存它,然后调用用于将其导入媒体库的import方法.

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.

例如,用于站点"MySite"的名为"Site Images"的媒体库将在磁盘上的/MySite/media/Site Images/上有一个文件夹.将文件放到那里(如果需要,可以使用子文件夹).此时,文件位于媒体库中,但尚未导入,因此您将无法使用它.通过在CMS Desk界面中查看媒体库,您可以看到这是正确的.但是,该文件尚未导入媒体库,您应该在新文件旁边的黄色三角形内看到一个感叹号.

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.

因此,在将文件放置在正确的位置后,您可以使用该文件信息填充MediaFileInfo对象并导入文件.

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.

这篇关于Kentico-将MemoryStream文件传递给MediaFileInfo API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆