나는 인해 파일ASP .NET에서 파일을 쓰는 중 오류를 처리하는 방법은 무엇입니까?
모든 코드가 try/catch 예외 블록
실제로 파일을 작성하는 Response.TransmitFile를 사용하고, 실패시에 둘러싸여를 전송하도록되어있는 페이지를 (이 클라이언트에 대한 응답 문제는 클라이언트에게 자동으로 생성 된 html입니다. 한 부분이 "현재 웹 요청을 실행하는 동안 처리되지 않은 예외가 생성되었습니다."라는 오류가 자세히 설명되어 있습니다.
왜 처리되지 않은 예외가 발생합니까?
파일이 성공적으로 처리 되었다면 헤더가 text/html로 변경 되었기 때문에 오류가 발생했습니다. 그러나 TransmitFile에 대한 호출이 응답에 자체 콘텐츠를 작성한 다음 플러시하는 것처럼 보이기 때문에 실제로 바람직하지 않습니다.
어떻게해야합니까?
try
{
String targetFile = Request.Form["filePath"];
if (targetFile == null) throw new Exception("No filename provided");
FileInfo file = new FileInfo(targetFile);
if (!file.Exists)
{
// file not found error
throw new Exception("File not found");
}
Response.ContentType = "APPLICATION/OCTET-STREAM";
Response.AppendHeader("Content-Disposition", "Attachment; Filename=\"" + Path.GetFileName(targetFile) + "\"");
Response.TransmitFile(file.FullName);
}
catch (Exception e)
{
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = "text/html";
StringBuilder sb = new StringBuilder();
// I write my own response in sb - I never see this content sent back!!
Response.Write(sb.ToString());
Response.Flush();
}
** file.FullName **의 값이 무엇 ? – Win
파일에 대한 Windows UNC 경로 (예 : \\ server \ folder \ file.ext)입니다. – Trant
Response.TransmitFile 아래에 다른 코드가 있습니까? 또한 헤더와 전송 파일을 추가하기 전에 응답을 지우지 않는 이유는 무엇입니까? –