首页 新闻 会员 周边

MVC文件流下载

0
悬赏园豆:10 [已解决问题] 解决于 2014-12-19 09:07

请教文件流下载:为了使下载的文件名不同于上传时的名。

代码:

public string WriteFile(string filepath, Guid id)
{
var xiangmu = BaoHuXiangMuZongKuService.GetEntity(p => p.BaoHuXiangMuZongKu_ID == id);
string createPath = System.Web.HttpContext.Current.Server.MapPath("/download/yusuangoucheng/");
Encoding code = Encoding.GetEncoding("utf-8");
// 读取上传的文件
string templateHTML = System.Web.HttpContext.Current.Server.MapPath(filepath);
StreamReader sr = null;
StreamWriter sw = null;
string str = string.Empty;
try
{
sr = new StreamReader(templateHTML, code);
str = sr.ReadToEnd();
}
catch (Exception ex)
{
throw new Exception("ReadFile Error:" + ex.Message + ex.InnerException + ex.StackTrace);
sr.Close();
return null;
}

string FileName = xiangmu.BaoHuXiangMuZongKu_NO + xiangmu.BaoHuXiangMuZongKu_Name + "预算" + Path.GetExtension(filepath);

// 写HTML文件
try
{
sw = new StreamWriter(createPath + FileName, true, code);
sw.Write(str);
sw.Flush();
}
catch (Exception ex)
{
throw new Exception("WriteFile Error:" + ex.Message + ex.InnerException + ex.StackTrace);
sw.Close();
return null;
}
finally
{
sw.Close();
}
return createPath + FileName;
}

现在的问题:写入的PDF文件打开是空的,里面什么也没有。.rar格式的文件有损坏。

927923690的主页 927923690 | 初学一级 | 园豆:93
提问于:2014-12-04 10:03
< >
分享
最佳答案
0

肯定是读取文件流里面出问题了,debugger一下。看看具体原因!

收获园豆:10
Mr.Brian | 小虾三级 |园豆:1518 | 2014-12-04 11:32

读取结果是这样的一串:

PK



\0\0\0\b\0\\墱D鞆??\0\0)


\0\0\0\0docProps/app.xml ?\0(?\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0漅薔?鼤(麈粒叒5B衾仐砸?g?鄄稶鹠?~??缻溂3爿賶?>g7塿?梑‑鏘G ?辅骜搜U|CfTM_碩爲儔?a?溓5Ιa5丛$V!,YJ軷触甊Y枩罛瞞\v[1]映,d疀Y?\nL荃j魁`?\n(F{?hUC葥胄$吥v?\t瘨H?o乨侅k?悏g沦??C尾\v弴梳w5諗⊥撲诔


蘄n昷8


'彍iid壯尺+r濣ァ瘦?/乵5荂gs??謀瘟睛暒n?z礼KF赋箲?间匇昫8愹\r?]萃|7痡%.蘏嚐`6毳⑻滔'?g\t(飒C{=?黯\r槠统MD臔鶙雫?<x抇&欭帀­?T樔7PK



\n\0\0\0\0\0\\墱DZ?艦[1]\0\0?\0\0\v\0\0_rels/.rels ?\0(?\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0锘??xml version=\"1.0\" encoding=\"utf-8\"?><Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\"><Relationship Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties\" Target=\"/docProps/app.xml\" Id=\"rId3\" /><Relationship Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument\" Target=\"/word/document.xml\" Id=\"R1e06d59c1f2b497f\" /><Relationship Type=\"http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties\" Target=\"/package/services/metadata/core-

 什么类型的文件都提示损坏,是不是写入有问题?实在找不到原因.....

927923690 | 园豆:93 (初学一级) | 2014-12-04 14:23

@927923690: 你可以参考一下这个文章。看到你读取文件里面有乱码,肯定是文件编码的问题。

.NET文件的默认编码也是Unicode。除非另外指定,StreamReader的默认编码为Unicode,

而不是当前系统的ANSI代码页。但是文档大部分还是以ANSI编码存储,中文文本使用的是GB2312,所以才造成中文乱码

所以在读取文本的时候要指定编码格式。要不把你的文件发送到我的邮箱,我给你读取一下看看。

c# 读取文件

c#中文乱码

Mr.Brian | 园豆:1518 (小虾三级) | 2014-12-04 15:05

@Mr.Brian: 

 1  public string WriteFile(string filepath, Guid id)
 2         {
 3           
 4             string FileName = "预算" + Path.GetExtension(filepath);
 5             string localpath = GlobalStatic.LocalTempFilePath(Path.GetExtension(filepath).Replace(".",""), "~/download/yusuangoucheng/",  "预算");
 6             string createPath = System.Web.HttpContext.Current.Server.MapPath("/download/yusuangoucheng/");
 7             Encoding code = Encoding.GetEncoding("utf-8");
 8             // 读取上传的文件  
 9             string templateHTML = System.Web.HttpContext.Current.Server.MapPath(filepath);
10             StreamReader sr = null;
11             StreamWriter sw = null;
12             string str = string.Empty;
13             try
14             {
15                 sr = new StreamReader(templateHTML,code);//(templateHTML, code)
16                 
17                 str=sr.ReadToEnd();
18             }
19             catch (Exception ex)
20             {
21                 throw new Exception("ReadFile Error:" + ex.Message + ex.InnerException + ex.StackTrace);
22                 sr.Close();
23                 return null;
24             }
25             // 写入文件  
26             try
27             {
28                 sw = new StreamWriter(createPath + FileName,false,code);
29                 sw.Write(str);
30                 sw.Flush();
31             }
32             catch (Exception ex)
33             {
34                 throw new Exception("WriteFile Error:" + ex.Message + ex.InnerException + ex.StackTrace);
35                 sw.Close();
36                 return null;
37             }
38             finally
39             {
40                 sw.Close();
41             }
42             return localpath.Replace("~", "");
43         }
44 
45 
46   public static string LocalTempFilePath(string ext, string path, string name)
47         {
48             string filepath = path + "/" + name + "." + ext;      //ext范例:xlsx;  
49 
50             return filepath;
51         }

这些代码,能帮我调一下吗?谢了!

927923690 | 园豆:93 (初学一级) | 2014-12-04 15:38

@927923690: ok,稍等啊!

Mr.Brian | 园豆:1518 (小虾三级) | 2014-12-04 15:41

@Mr.Brian:我这边试了一下你的读取文件的代码,我在c盘建了一个test.txtde 文件里面的内容是

hello word
小李子

然后我通过你的方式指定UTF-8格式去读取文件,结果如下图所示。

然后我改过代码之后,

这是读取文件的问题,其他的我还没仔细调试,我就大体看了一下,并不是所有的文件格式都要按照UTF-8格式读取文件,而是你要采用和你读取文件的编码格式一样,这样才不会有问题,不会出现乱码。至于细节问题自己想去吧!

Mr.Brian | 园豆:1518 (小虾三级) | 2014-12-04 16:25

@Mr.Brian: 你写的很细,谢谢你。

927923690 | 园豆:93 (初学一级) | 2014-12-04 16:57

@927923690: 客气了,共同学习!

Mr.Brian | 园豆:1518 (小虾三级) | 2014-12-04 16:59
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册