我正在使用Response.TransmitFile()从我的C盘上的文件夹下载一个zip文件夹。文件夹下载正常,我的文件出现在我的下载文件夹中。但是问题出在我的下载文件夹中,其zip文件的名称为asp页面,而inside是我想下载的文件夹。另一个问题是我在上传的zip文件夹末尾附加了一个DataTime,但日期不在文件夹名称的末尾。
我的上传代码如下所示:
string pnq = HttpContext.Current.Request.Url.PathAndQuery;
string url = HttpContext.Current.Request.Url.AbsoluteUri.Replace(pnq, "/");
if (FileUpload1.HasFile)
{
var filename = FileUpload1.PostedFile.FileName;
var uriID = Guid.NewGuid().ToString();
var password = System.Web.Security.Membership.GeneratePassword(7, 2);
filename = filename.Remove(filename.Count() - 4) + "-" + DateTime.Now.ToShortDateString() + ".zip";
filename = filename.Replace(" ", "-");
filename = filename.Replace("/", "-");
FileUpload1.SaveAs("C:\\Uploads\\" + filename);
lblUri.Text = url + "UICDownload.aspx?fileID=" + uriID;
lblPassword.Text = password;
string file = MapPath("~/Sample.xml");
XDocument doc = XDocument.Load(file);
doc.Root.Add(new XElement("File", new XElement("name", filename), new XElement("uriID", uriID), new XElement("password", password)));
XElement name = new XElement("name", filename);
doc.Save(file);
}
我的下载代码如下所示:
var text = Request.QueryString["fileID"];
string file = MapPath("~/Sample.xml");
XDocument doc = XDocument.Load(file);
var node = doc.Document.Descendants("uriID").FirstOrDefault(u => u.Value.Equals(text));
var filenode = node.Ancestors("File").First();
var tempname = filenode.Element("name");
var filename = tempname.Value.ToString();
var filePassword = filenode.Element("password");
if (filePassword.Value.ToString() == tbPassword.Text)
{
Response.Clear();
Response.ContentType = "application/zip";
Response.AppendHeader("Content-Disposition", "attachment; fileID=" + text);
Response.TransmitFile("C:\\Uploads\\" + filename);
Response.End();
}
我保存的XML文档如下所示:
<?xml version="1.0" encoding="utf-8"?>
<rootElement>
<File>
<name>Pictures-21-06-2013.zip</name>
<uriID>96e1253b-634b-498a-b062-61a1a097ee3f</uriID>
<password>%zFxRr|</password>
</File>
<File>
<name>Test1-21-06-2013.zip</name>
<uriID>44d3d2c8-5c19-4f79-a5e2-66bb023a4d5e</uriID>
<password>{hik6.e</password>
</File>
欢迎任何建议,如果您希望我显示任何其他代码,请告诉我们。还要添加,当文件上传到C:\ Uploads文件夹时,zip文件夹的名称末尾有日期。
答案 0 :(得分:1)
尝试将此行更改为以下内容:
Response.AppendHeader("content-disposition", "attachment; filename=" + filename);