无法下载文件.EPS编程 [英] Unable to download .eps file programmatically

查看:139
本文介绍了无法下载文件.EPS编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个code从中用户可以下载该文件。这将下载的文件将只用于图像文件。

I have created a code from which the user is able to download the file. The files which will be downloaded will be used for image files only.

我的code是按如下。

My code is as per below.

try
{
    System.IO.FileInfo fileInfo = new FileInfo(file.Name);
    // replace special characters with blank.
    string filename = GeneralMethods.MakeValidFileName(Path.GetFileNameWithoutExtension(file.Name));
    filename += fileInfo.Extension;
    byte[] obj = (byte[])file.OpenBinary();
    Response.Clear();
    Response.ClearContent();
    Response.ClearHeaders();
    Response.Cache.SetCacheability(System.Web.HttpCacheability.Private);
    Response.AppendHeader("Content-Disposition", "attachment; filename= " + filename);
    Response.ContentType = MimeMapping.GetMimeMapping(filename);
    if (Response.IsClientConnected)
        Response.BinaryWrite(obj);
    Response.Flush();
   // Response.Close();
    HttpContext.Current.ApplicationInstance.CompleteRequest();

}
catch (Exception ex)
{       
}
finally
{

}

当我下载 .EPS 使用此$ C $文件c不能打开客户端应用程序的文件。

When I download .eps file using this code it cannot open the file in the client application.

我是什么在我的code失踪?

What am I missing in my code?

推荐答案

尝试不同的code招数后,终于找到了答案。

After trying various code tricks finally found an answer.

我在我的code唯一缺少的就是定义响应对象中的内容长度标头。

The only thing I was missing in my code was to define the "Content-Length" header in Response object.

所以,code是按如下:

So the code is as per below:

try
{
    System.IO.FileInfo fileInfo = new FileInfo(file.Name);
    string filename = GeneralMethods.MakeValidFileName(Path.GetFileNameWithoutExtension(file.Name));
    filename += fileInfo.Extension;
    byte[] obj = (byte[])file.OpenBinary();
    Response.Clear();
    Response.ClearContent();
    Response.ClearHeaders();
    Response.Cache.SetCacheability(System.Web.HttpCacheability.Server);
    var cd = new System.Net.Mime.ContentDisposition { FileName = file.Name, Inline=false};
    Response.AppendHeader("Content-Disposition", cd.ToString());
    Response.AppendHeader("Content-Length",obj.Length.ToString());
    Response.ContentType = MimeMapping.GetMimeMapping(filename);                
    if (Response.IsClientConnected)
        Response.BinaryWrite(obj);
    Response.Flush();
   // Response.Close();
    HttpContext.Current.ApplicationInstance.CompleteRequest();
}
catch (Exception ex)
{       
    Response.Redirect(Request.UrlReferrer.ToString());
}
finally
{
}

现在它的工作在我的情况... !!! : - )

Now it's working in my case...!!! :-).

希望这有助于其他太..

Hope this helps other too..

感谢

这篇关于无法下载文件.EPS编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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