protected void ExportFile_Click(object sender, EventArgs e)
{
//게시물에서 선택된 파일을 다운로드 위한 메서드
string filename = "nodeReserveExcelfile.xlsx";
string downPath = Server.MapPath(ConfigurationManager.AppSettings["FilePath"]) + "\\" + filename; //다운로드할 파일경로
if (System.IO.File.Exists(downPath)) // 서버에 파일이 존재할 경우
{
Response.Clear();
Response.AddHeader("Content-Type", "application/unknown");
// web.config 가 <globalization requestEncoding="ks_c_5601-1987" responseEncoding="ks_c_5601-1987" /> 경우
Response.AddHeader("Content-Disposition", "attachment;filename =" + HttpUtility.UrlEncode(System.IO.Path.GetFileName(filename)).Replace("+", "%20"));
// web.config 가 <globalization requestEncoding="utf-8" responseEncoding="utf-8" />경우
//Response.AddHeader("Content-Disposition","attachment;filename="+Server.UrlEncode(Path.GetFileName(selectedItemName)).Replace("+","%20"))
Response.ContentType = "multipart/form-data";
Response.WriteFile(downPath);
Response.Flush();
Response.End();
return;
}
else// 서버에 파일이 존재하지 않을 경우
{
//files.Attributes.Add("onclick", string.Format("alert('{0}');", "파일이 서버에 존재하지 않습니다")); //파일이 서버에 존재하지 않습니다.
Response.Write("");
return;
}
}
--web에서 파일 다운로드 두번째 방법--
public void DownloadProgram(string pluginId, string pluginVerId)
{
HttpResponse response = HttpContext.Current.Response;
try
{
DataTable dtPluginVersion = null;
string filePath = string.Empty;
string programNm = string.Empty;
IPluginVersionBiz biz = ContextFactory.GetObject<IPluginVersionBiz>("PluginVersionBiz");
dtPluginVersion = biz.GetPluginProgram(pluginId, pluginVerId);
if (dtPluginVersion != null && dtPluginVersion.Rows.Count > 0)
{
programNm = (string)dtPluginVersion.Rows[0]["sProgramName"];
filePath = (string)dtPluginVersion.Rows[0]["sUpdUrl"];
if (!string.IsNullOrEmpty(filePath))
filePath = WebConfigurationManager.AppSettings["upload"] + filePath;
}
response.BufferOutput = false;
response.Clear();
response.ClearHeaders();
response.ContentType = "application/octet-stream";
response.AddHeader("Content-Disposition", "attachment; filename=" + programNm);
response.AddHeader("Connection", "close");
if (!string.IsNullOrEmpty(filePath))
{
FileInfo file = new FileInfo(filePath);
if (!file.Exists)
throw new FileNotFoundException(programNm);
byte[] buffer = new byte[4098];
int readBytes = 0;
using (FileStream stream = file.OpenRead())
using (Stream os = response.OutputStream)
{
while ((readBytes = stream.Read(buffer, 0, buffer.Length)) > 0)
{
if (response.IsClientConnected)
{
os.Write(buffer, 0, readBytes);
os.Flush();
}
else
{
break;
}
}
}
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
response.StatusCode = (int)HttpStatusCode.InternalServerError;
response.Write("");
}
finally
{
response.Flush();
response.Close();
response.End();
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////