2010-01-26 55 views
0

從服務方法返回我打開瀏覽器從一個文件:如何使用asp.net C#代碼

String FileName, 
Byte[] FileData, 
string FileType(includes: doc, pdf, tif, tiff, gif, jpg, jpeg, png, bmp, wpd) 

我怎麼能基於文件類型文件並將其展示給用戶的瀏覽器?下載到用戶對我來說可以。

回答

2
// You will need to figure out the correct content type based on the file type 
// for example image/jpeg for jpeg files 
Response.ContentType = ...; 
var cd = new ContentDisposition() 
{ 
    Inline = true, 
    FileName = FileName 
}; 
Response.AppendHeader("Content-Disposition", cd.ToString()); 
Response.OutputStream.Write(FileData, 0, FileData.Length); 
+1

要完成,我會在寫入後添加一個Response.Flush()和Response.Close()。 – 2010-01-26 16:46:37