1
ストリーミングされた画像をInternet ExplorerやGoogle Chromeに表示するのに少し問題がありますが、FireFoxではうまく表示されます。私は下に私のコードを貼り付けて、私はビットと私はgoogleingを見つけたボブの負荷を使用して一緒に置いた。MVC 3 Internet ExplorerやChromeでストリーミングされた画像を表示する
public ImageResult GetPhotoS(string photoID, int userID, int? galleryID)
{
if (galleryID == null)
{
string thumbLocation = string.Format("{0}{1}\\Pics\\{2}_thumb.jpg", ConfigurationManager.AppSettings["PhotoLocation"].ToString(), Convert.ToInt32(User.Identity.Name), photoID);
using (FileStream stream = new FileStream(thumbLocation, FileMode.Open))
{
FileStreamResult fsResult = new FileStreamResult(stream, "image/jpeg");
ImageResult result = new ImageResult(ReadFully(fsResult.FileStream), "image/jpeg");
return result;
}
}
}
private static byte[] ReadFully(Stream input)
{
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
public class ImageResult : ActionResult
{
public String ContentType { get; set; }
public byte[] ImageBytes { get; set; }
public String SourceFilename { get; set; }
//This is used for times where you have a physical location
public ImageResult(String sourceFilename, String contentType)
{
SourceFilename = sourceFilename;
ContentType = contentType;
}
//This is used for when you have the actual image in byte form
// which is more important for this post.
public ImageResult(byte[] sourceStream, String contentType)
{
ImageBytes = sourceStream;
ContentType = contentType;
}
public override void ExecuteResult(ControllerContext context)
{
var response = context.HttpContext.Response;
response.Clear();
response.Cache.SetCacheability(HttpCacheability.NoCache);
response.ContentType = ContentType;
//Check to see if this is done from bytes or physical location
// If you're really paranoid you could set a true/false flag in
// the constructor.
if (ImageBytes != null)
{
var stream = new MemoryStream(ImageBytes);
stream.WriteTo(response.OutputStream);
stream.Dispose();
}
else
{
response.TransmitFile(SourceFilename);
}
}
}
私は、次を使用して画像を表示しています:
<img src="@Url.Action("GetPhotoS", "Image", new { photoID = photo.ID, userID = Convert.ToInt32(User.Identity.Name) })" alt="@photo.Description" />
私はChromeとIEから取得するすべての画像があるべき通常の赤い十字です。どんな助けもありがとう。
「ImageResult」とは何ですか? 'ReadFully'とは何ですか?実際のコードを表示してください。私はあなたのコードを表示せずにどのような答えを期待しているのだろうか。 –
私は余分なコードを追加しました。私はそれを隠そうとしていないことを謝りました。 –
'image/jpeg'のコンテンツタイプを使用しようとしましたか?私は、 'image/jpg'がすべてのブラウザで有効であると広く受け入れられているかどうかはわかりません。 –