1.上传
if (this.FileUpload1.PostedFile != null)
{
string strfileName = this.FileUpload1.PostedFile.FileName;
if (!string.IsNullOrEmpty(strfileName.Trim()))
{
string strLastPath = getSaveImage(strfileName);
//保存
this.FileUpload1.SaveAs(strLastPath);
content.A8 = System.IO.Path.GetFileName(strLastPath);
}
}
#region
if (this.FileUpload1.PostedFile != null)
{
string strfileName = this.FileUpload1.PostedFile.FileName;
string str = strfileName.ToLower().Substring(strfileName.LastIndexOf("."));
if (str != ".zip" && str != "rar")
{
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "click", "alert('上传文件必须是rar或zip格式的!');", true);
return;
}
if (!string.IsNullOrEmpty(strfileName.Trim()))
{
string strLastPath = getSaveImage(strfileName);
//保存
this.FileUpload1.SaveAs(strLastPath);
content.A8 = System.IO.Path.GetFileName(strLastPath);
}
}
#endregion
#region getSavefile
/// <summary>
/// getSaveImage
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
string getSaveImage(string fileName)
{
string strGuid = System.Guid.NewGuid().ToString();
return Server.MapPath("~/ZB_File/") + strGuid + System.IO.Path.GetExtension(fileName);
}
#endregion
2.下载
WebClient webClient = new WebClient();
string fileName = Server.MapPath("~/ZB_File/" + Tb_ContentManager.GetContenByLId(Convert.ToInt32(hid_lid.Value)).A8);
byte[] responseData;
try
{
responseData = webClient.DownloadData(fileName);
}
catch
{
return;
}
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = System.IO.Path.GetExtension(fileName);
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length", responseData.Length.ToString());
if (responseData.Length > 0)
{
Response.BinaryWrite(responseData);
}
Response.Flush();
Response.End();