[英]Which content type should use for all types of files to download file in asp.net?
我具有在asp.net中下載文件的功能。 我有字節,文件名和大小。 我嘗試通過以下方式進行操作,但是每當我打開文件時,它都無法正確打開。 如果是jpg或png文件,則不會顯示圖片。
這是我點擊按鈕時的代碼:
byte[] bytes = Encoding.ASCII.GetBytes(getStringFromServer());
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
我已經讀過需要指定ContentType的地方。 但就我而言,文件可以是任何類型。
有人可以指導我適當的方法嗎?
我也遇到同樣的問題,並用非常簡單的方法解決它,通常您的文件名具有文件類型,所以我使用以下代碼:
string fileType = fileName.Substring(fileName.LastIndexOf(".") + 1);
if (FileFetchManager.IMAGE_TYPES.Contains(fileType, StringComparison.OrdinalIgnoreCase, true))
HttpContext.Current.Response.ContentType = "image/" + FileFetchManager.ConvertFileExtensionToImageFormat(fileType).ToString();
else
HttpContext.Current.Response.ContentType = "application/" + fileType;
這是我的FileFetchManager類:
public class FileFetchManager
{
public const string DEFAULT_FILE_TYPE = "txt";
public static readonly string[] IMAGE_TYPES = { "bmp", "jpg", "jpeg", "png", "gif", "tiff", "tif" };
public static string GetFileFetchUrl(string TableName, string FieldName, string FileNameFieldName, string PrimaryKeyValue)
{
return GetFileFetchUrl(TableName, FieldName, FileNameFieldName, PrimaryKeyValue, null);
}
public static string GetFileFetchUrl(string TableName, string FieldName, string FileNameFieldName, string PrimaryKeyValue, int? thumbnailImageSize)
{
return QueryStringCoder.CodeUrl("~/UserControl/JcoDataGrid/DBFileFetch.aspx?t=" + TableName + "&f=" + FieldName + "&fnf=" + FileNameFieldName + "&i=" + PrimaryKeyValue + (thumbnailImageSize == null ? "" : "&s=" + thumbnailImageSize));
}
public static string GetFileFetchUrl(string fileBytesSessionName, string fileName)
{
return GetFileFetchUrl(fileBytesSessionName, fileName, null);
}
public static string GetFileFetchUrl(string fileBytesSessionName, string fileName, int? thumbnailImageSize)
{
return QueryStringCoder.CodeUrl("~/UserControl/JcoDataGrid/MemoryFileFetch.aspx?sn=" + fileBytesSessionName + "&fn=" + fileName + (thumbnailImageSize == null ? "" : "&s=" + thumbnailImageSize));
}
public static string GetFileType(string fileName)
{
if (fileName.IsNullOrWhiteSpace())
return DEFAULT_FILE_TYPE;
return fileName.Substring(fileName.IndexOf(".") + 1);
}
public static bool IsImage(string fileName)
{
string fileType = fileName.Substring(fileName.LastIndexOf(".") + 1);
bool isImage = FileFetchManager.IMAGE_TYPES.Contains(fileType, StringComparison.OrdinalIgnoreCase, true);
return isImage;
}
public static byte[] GetThumbnailImage(string fileName, byte[] imageBytes, int size)
{
if (size <= 0)
throw new Exception("ErrorCode : 1390/10/23-22:04 : " + QueryStringCoder.Code("اندازه درخواستي جهت تامبنيل تصوير(" + size + ") مجاز نمي باشد!"));
ImageFormat imageType = ConvertFileExtensionToImageFormat(GetFileType(fileName));
MemoryStream ms = new MemoryStream(imageBytes);
ms.Position = 0;
System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
int width;
int height;
if (img.Height > img.Width)
{
height = size.ToInt();
width = img.Width * size.ToInt() / img.Height;
}
else //if (img.height <= img.width)
{
width = size.ToInt();
height = img.Height * size.ToInt() / img.Width;
}
if (imageType == ImageFormat.Png || imageType == ImageFormat.Gif)
{
System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
img = img.GetThumbnailImage(width, height, myCallback, IntPtr.Zero);
imageType = ImageFormat.Png;
}
else
img = ImageResizer.ConstrainProportions(img, height, Dimensions.Height);
MemoryStream thumbnailImageStream = new MemoryStream();
thumbnailImageStream.Position = 0;
img.Save(thumbnailImageStream, imageType);
return thumbnailImageStream.ToArray();
}
public static bool ThumbnailCallback()
{
return false;
}
public static ImageFormat ConvertFileExtensionToImageFormat(string fileExtension)
{
switch (fileExtension.ToLower())
{
case "jpeg":
case "jpg":
return ImageFormat.Jpeg;
case "bitmap":
case "bmp":
return ImageFormat.Bmp;
case "gif":
return ImageFormat.Gif;
case "icon":
case "ico":
return ImageFormat.Icon;
case "png":
return ImageFormat.Png;
case "tiff":
case "tif":
return ImageFormat.Tiff;
default:
throw new NotImplementedException();
//break;
}
}
public static void SetResposeContentType(string fileName)
{
string fileType = fileName.Substring(fileName.LastIndexOf(".") + 1);
if (FileFetchManager.IMAGE_TYPES.Contains(fileType, StringComparison.OrdinalIgnoreCase, true))
HttpContext.Current.Response.ContentType = "image/" + FileFetchManager.ConvertFileExtensionToImageFormat(fileType).ToString();
else
HttpContext.Current.Response.ContentType = "application/" + fileType;
}
}
我希望它對您有用
內容類型的目的是允許用戶代理(UA)確定下載資源的類型。 沒有內容類型,下載的內容僅僅是裸數據,可能是任何內容。
在某些情況下可能會推斷出內容類型,但這在很大程度上取決於上下文。 如果內容是圖像,則UA可以推斷圖像類型,如果內容是文本,則可以檢查字節順序標記等。但是由於數據幾乎是任何東西,UA不可能這樣做自卑。
您已指定內容類型application/octet-stream
。 這意味着您要告訴UA內容是二進制數據。 UA就是這樣對待它的。 如果要將數據識別為圖像,則需要指定正確的內容類型。 application/octet-stream
還不夠。
當您存儲任何類型的數據時,絕對有必要將MIME類型與數據一起存儲。 如果從光盤讀取數據,則可以從文件擴展名中識別內容類型,但是服務器需要這樣做,因為UA無法這樣做。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.