표시 광고 내용에서 Respose.WriteFile()/응답입니다.ContentType

StackOverflow https://stackoverflow.com/questions/3234

  •  08-06-2019
  •  | 
  •  

문제

어떻게 하나의 디스플레이에 어떤 콘텐츠를 추가에서는"다이나믹"aspx?현재 저는 작업에 사용하는 시스템입니다.웹.HttpResponse"페이지를 참조하십시오."응답을 작성하는 파일에 저장된 웹 서버에는 웹 요청을 합니다.

이 사람들을 칠한 url 형식 http://www.foo.com?Image=test.jpg 과 이미지에서 자신의 브라우저입니다.그래서 당신이 이것을 알고 있 사용으로 돌아가지의 반응이다.ContentType.

를 사용하여

Response.ContentType = "application/octet-stream";

나는 이미지를 표시하는 유형의 gif/jpeg/png(모든 나는 지금까지 테스트),조금을하려고합니다.는 swf 또는.ico 파일을 나에게 좋은 작은 오류가 있습니다.

Response.ContentType = "application/x-shockwave-flash";

I 을 얻을 수 있는 플래시를 재생하는 파일이지만,그런 다음 이미지가 엉망.

그래서 어떻게 선택 contenttype?

도움이 되었습니까?

해결책

이긴 하지만,가장 좋은 방법은 보는 파일의 콘텐츠 형식을 설정합니다.

switch ( fileExtension )
{
    case "pdf": Response.ContentType = "application/pdf"; break; 
    case "swf": Response.ContentType = "application/x-shockwave-flash"; break; 

    case "gif": Response.ContentType = "image/gif"; break; 
    case "jpeg": Response.ContentType = "image/jpg"; break; 
    case "jpg": Response.ContentType = "image/jpg"; break; 
    case "png": Response.ContentType = "image/png"; break; 

    case "mp4": Response.ContentType = "video/mp4"; break; 
    case "mpeg": Response.ContentType = "video/mpeg"; break; 
    case "mov": Response.ContentType = "video/quicktime"; break; 
    case "wmv":
    case "avi": Response.ContentType = "video/x-ms-wmv"; break; 

    //and so on          

    default: Response.ContentType = "application/octet-stream"; break; 
}

다른 팁

이 솔루션의 일부가 사용하는 현지 인트라넷에 있습니다.의 일부 변수가 있을 것이 자신을 수집하는 내가 그들을 끌어 데이터베이스에서 하지만 당신은 수도에서 그들을 끌어요.

만 추가하지만 나도 거기에 있는 라는 함수 getMimeType 는 데이터베이스를 연결하고 다시 끌어 올바른 내 유형에 따라 파일의 확장입니다.이 기본값 application/octet-stream 별도로 명시되지 아니하는 경우 발견했다.

// Clear the response buffer incase there is anything already in it.
Response.Clear();
Response.Buffer = true;

// Read the original file from disk
FileStream myFileStream = new FileStream(sPath, FileMode.Open);
long FileSize = myFileStream.Length;
byte[] Buffer = new byte[(int)FileSize];
myFileStream.Read(Buffer, 0, (int)FileSize);
myFileStream.Close();

// Tell the browse stuff about the file
Response.AddHeader("Content-Length", FileSize.ToString());
Response.AddHeader("Content-Disposition", "inline; filename=" + sFilename.Replace(" ","_"));
Response.ContentType = getMimeType(sExtention, oConnection);

// Send the data to the browser
Response.BinaryWrite(Buffer);
Response.End();

Yup Keith 긴 하지만 사실이다.나는 끝났을 배치하는 MIME 유형을 우리가 사용하는 것이 데이터베이스로 한 다음 그들을 끌어올 때 내가 게시한 파일입니다.도저히 믿을 수 없는 일이 없다는 것을 autoritive 목록의 형식을 밖에 없다는 것을 언급에서 가능한 것이 무엇인지에 MSDN.

내가 발견 사이트 제공하는 몇 가지 도움이됩니다.

습니다.Net4.5 하나 사용할 수 있습니다

MimeMapping.GetMimeMapping

그것을 반환합니다 MIME 매핑 지정한 파일에 대한 이름입니다.

https://msdn.microsoft.com/en-us/library/system.web.mimemapping.getmimemapping(v=vs.110).aspx

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top