Kentico - 将 MemoryStream 文件传递​​给 MediaFileInfo API

发布于 2024-12-03 08:59:04 字数 1349 浏览 0 评论 0原文

我创建了一个 iTextSharp PDF 文件,该文件创建到 MemoryStream。但我现在需要将此文件传递到 Kentico 媒体库。

如果有人能告诉我如何做到这一点,我将不胜感激。我当前的代码是:

//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,我找不到提供所有这些信息的方法。

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

原来是傀儡 2024-12-10 08:59:04

事实上,我认为你不需要做 RadekM 所说的任何事情。您只需将文件流式传输到磁盘进行保存,然后调用您使用的导入方法将其导入媒体库。

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

因此,在正确位置获取文件后,您可以使用该文件信息来填充 MediaFileInfo 对象并导入文件。

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.

中性美 2024-12-10 08:59:04

您可以修改此代码并从此处传递 PDF 的字节吗?

以编程方式将文件添加到 Kentico 媒体库

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

programmatically adding files to the Kentico Media Library

萌能量女王 2024-12-10 08:59:04

遗憾的是,MemoryStream 类不包含这些信息,因此您无法直接从该对象获取它们。无论如何,如果您想提供 FileSize 属性,可以使用 ms.Length 属性作为解决方法。基本上,这个特定属性并不重要,因此它甚至可以是一些虚拟数字。
至于扩展名 - 您是说您收到错误消息,指出该属性为空,尽管您将其设置为“fileInfo.FileExtension = ".pdf";“?你能澄清一下吗?
另请注意,您需要设置一些其他属性,FileSiteID、FileCreatedWhen、FileGUIDFilePath(给定媒体库内的路径)。如果您有 Kentico API 的完整源代码,您可以从 \MediaLibrary\MediaFileInfo.cs 类中的 MediaFileInfo 对象的构造函数中获得灵感。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文