如何直接从与写入磁盘相反的MemoryStream中打开文件(pdf文件) [英] How to open a file directly from a MemoryStream opposed to writing to disk (pdf file)

查看:345
本文介绍了如何直接从与写入磁盘相反的MemoryStream中打开文件(pdf文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在用C#编写代码,并且已经使用itextsharp创建了pdf,
现在我想直接在Acrobat Reader 9中打开我的pdf文件(不提供文件流的任何路径),然后询问客户,他想保存该pdf文件.

Hi
I am doing code in C#, and i have created pdf using itextsharp,
now i want to open my pdf directly in Acrobat Reader 9 (without giving any path to filestream)and then ask to client , wheather he want to save that pdf file.

MemoryStream ms = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(pDoc, ms); 

var file = System.IO.Path.GetTempFileName();
           using (var fileStream = File.OpenWrite(file))
           {
               var buffer = ms.GetBuffer();
               fileStream.Write(buffer, 0, (int)buffer.Length);
           }



我已经使用了上面的代码,但是用户代码未处理出错SecurityException -不允许文件操作.路径"的访问被拒绝

当我使用下面的代码创建den pdf时,意味着管理员设置没有任何问题.



I have used above code, but getting error SecurityException was unhandled by user code -- File operation not permitted. Access to path '''' is denied

when i have used below code den pdf is getting created , means Admin settings have not any problems.

writer = PdfWriter.GetInstance(doc, new FileStream(filpath, FileMode.Create));



所以请帮助我,这样我就可以直接从memorystream打开pdf而不将其保存在磁盘中.



so plz help me , so that i will open direct pdf from memorystream without saving it in a disk.

推荐答案

我看不到您在哪里得到了这个异常.用获得的名称System.IO.Path.GetTempFileName()写入文件是完全合法的.如果文件名为空(错误消息的提示),则错误消息将有所不同(空路径名不合法").因此,我怀疑您的例外情况报告并非100%准确.不用担心-您不需要这样做.

首先,请了解内存流是完全无用的.是的,您可以对其进行读写,但是如何使Acrobat Reader 9做到这一点呢?该应用程序可能与.NET无关.

因此,不幸的是,您一定会使用临时文件.如果您的最后一个代码示例有效,请使用它,仅将变量filpath分配给临时文件名:
I cannot see where you got this exception. Writing to the file with the name obtained System.IO.Path.GetTempFileName() is perfectly legal. If the file name was empty (how your error message suggests), the error message would be different ("Empty path name is not legal"). So, I suspect your exception report is not 100% accurate. Not to worry — you don''t need to do this.

First, please understand that the memory stream is quite useless. Yes, you can read and write to it, but how can you make Acrobat Reader 9 doing it? This application probably has nothing to do with .NET.

So, unfortunately, you''re bound to use a temporary file. If your last code sample works, use it, only assign your variable filpath to temporary file name:
string filpath = System.IO.Path.GetTempFileName();
//...



—SA



—SA


这篇关于如何直接从与写入磁盘相反的MemoryStream中打开文件(pdf文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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