无法在ASP.NET C#中加载pdf [英] Failed to load pdf in ASP.NET C#

查看:80
本文介绍了无法在ASP.NET C#中加载pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string Article = Request.QueryString [Article];

> if(Article.Length> 6)

> {

> string pdfpath =D:\\Document \\Client \\DWG \\+

> Article.Substring(0,6)+\\+ Article +。pdf;

> string path = pdfpath;

> WebClient客户端=新WebClient();

>字节[]缓冲区= client.DownloadData(路径);

> if(buffer!= null)

> {

> Response.ContentType =application / pdf;

> Response.AddHeader(content-length,

> buffer.Length.ToString());

> Response.BinaryWrite(缓冲区);

> }

> }



我尝试了什么:



当我运行它时本地机器工作正常,但当我在服务器上部署它时,它给我上面提到的错误。 Plz help。

string Article = Request.QueryString["Article"];
> if (Article.Length > 6)
> {
> string pdfpath = "D:\\Document\\Client\\DWG\\" +
> Article.Substring(0, 6) + "\\" + Article + ".pdf";
> string path = pdfpath;
> WebClient client = new WebClient();
> Byte[] buffer = client.DownloadData(path);
> if (buffer != null)
> {
> Response.ContentType = "application/pdf";
> Response.AddHeader("content-length",
> buffer.Length.ToString());
> Response.BinaryWrite(buffer);
> }
> }

What I have tried:

when i run it on local machine its working fine but when i deploy it on server it gives me the above mentioned error. Plz help.

推荐答案

查看代码从中获取PDF文件的路径。在服务器上,D:驱动器将不存在。您犯了硬编码PDF文件路径而不是使用相对路径的错误。



PDF文件必须位于应用程序的子文件夹中或者您将不得不在服务器中专门设置安全性,以便您的代码可以访问其他文件夹中服务器上的文件。



在Web服务器上,您的代码在受限制的帐户下的沙箱中运行。它只能访问应用程序所在的文件夹以及该文件夹下的所有子文件夹。它不能访问服务器上的任何其他文件夹,也无权访问任何网络共享/驱动器。
Look at the path your code is getting the PDF files from. On the server, the D: drive won't exist. You made the mistake of hard coding the path to the PDF files instead of using a relative path.

The PDF files have to be in a sub-folder of your application or you're going to have to specially setup security in the server so your code can get to the files on the server in some other folder.

On a web server, your code runs in a sandbox under a restricted account. It'll have access only to the folder the app is in and any sub folders under that. It will NOT have access to any other folders on the server, nor will it have access to any network shares/drives.


这篇关于无法在ASP.NET C#中加载pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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