2012-09-21 2 views
1

거부 ​​나는액세스가

 var document = new Document(); 
     string path = Server.MapPath("AttachementToMail"); 
     PdfWriter.GetInstance(document, new FileStream(path + 
        "/"+DateTime.Now.ToShortDateString()+".pdf", FileMode.Create)); 

이 지금은이 문서

Response.ContentType = "Application/pdf"; 
Response.AppendHeader("Content-Disposition", "attachment; filename="+ 
           DateTime.Now.ToShortDateString() + ".pdf" + ""); 
Response.TransmitFile(path); 
Response.End(); 

를 다운로드 할 하나 개의 PDF 문서 생성하지만 그것은 나를 경로 '에 액세스 에러했다 ~ \ AttachementToMail '이 (가) 거부되었습니다. IIS_IUSRS에 대한

읽기/쓰기 액세스는

+1

Server.MapPath는 디렉토리의 절대 경로를 반환하지만 가상 경로를 제공하고 있습니다. 'TransmitFile'을 호출 할 때'path'를 확장 했습니까? – Leonard

+0

명백한 질문이지만 사이트를 실행하는 계정이 해당 폴더에 대한 액세스 권한을 갖고 있습니까? 또한 : 당신은 ** 명시 적으로 ** 어떤 라인을 던질 수 있습니까? 그것은'string path = Server.MapPath ("AttachementToMail")'입니까? 또한 : 그냥 오타 이슈입니까? 실제로'AttachmentToMail' 디렉토리입니까? –

+0

@MarcGravell 오류가 온라인 상태입니다. Response.TransmitFile (path); Response.TransmitFile (fullPath) 후 – Alex

답변

2

당신이 쓰는 제공하는 경로는 가상 경로 존재한다. TransmitFile은 절대 경로를 필요로합니다.

코드는 다음과 같이 보일 것이다 :

var document = new Document(); 
string path = Server.MapPath("AttachementToMail"); 
var fileName = DateTime.Now.ToString("yyyyMMdd")+".pdf"; 
var fullPath = path + "\\" + fileName; 

//Write it to disk 
PdfWriter.GetInstance(document, new FileStream(fullPath, FileMode.Create)); 

//Send it to output 
Response.ContentType = "Application/pdf"; 
Response.AppendHeader("Content-Disposition", "attachment; filename="+ fileName); 

Response.TransmitFile(fullPath); 
Response.Flush(); 
Response.End(); 

DateTime.Now는 현재 시간을 나타냅니다. 파일 이름으로 사용할 때는주의하십시오. ToShortDateString을 사용하는 것은 다소 위험합니다. 일부 문화권에서는 /을 해당 형식으로 사용합니다. ToString을 사용하면 서버 문화권에 관계없이 파일 이름 형식을 수정할 수 있습니다.

+0

; Response.End(); 문서가 다운로드되지 않습니다 – Alex

+0

'Response.Flush(); 앞에'Response.Flush();'넣기 – nunespascal

+0

Response.Flush();를 추가했습니다. 하지만 여전히 작동하지 않습니다. (문서가 생성되었지만 다운로드되지 않습니다. – Alex

관련 문제