MVCアプリケーションでは、STAスレッドでWebブラウザコントロールを使用しています。私は、STAスレッドの最後にクライアントにpngイメージを送信したいと思います。MVCのstaスレッドを使用してpngイメージをダウンロード
これは、私は、サーバー側でJavaScriptウィジェットを輸出の可能性をチェックしています私のコード
public void Export() //Action method for exporting
{
//Saving widget as image using web browser control
System.Threading.Thread tr = new System.Threading.Thread(() => ExportWidget());
tr.SetApartmentState(ApartmentState.STA);
tr.Start();
}
public void ExportWidget() //STA method for downloading
{
WebBrowser browser = new WebBrowser() { ScriptErrorsSuppressed=true, WebBrowserShortcutsEnabled = false, Size = new System.Drawing.Size(1000, 1000) };
browser.DocumentText = new StringBuilder()
//Add header for HTML document
.Append("<!DOCTYPE html><html><head><meta http-equiv='X-UA-Compatible' content='IE=edge' />"
//Add scripts required for rendering widget in browser control
+ AddScripts(browser, widgetOption)
+ "</head><body><div id='widgetContainer'></div></body></html>").ToString();
//Check browser is loaded or not. If it is not ready wait until browser loading is complete
while (browser.ReadyState == WebBrowserReadyState.Loading)
{
Application.DoEvents();
Thread.Sleep(5);
}
MemoryStream stream = new MemoryStream();
using (Bitmap img = new Bitmap(ParseInt(bounds[1]), ParseInt(bounds[0])))
{
browser.DrawToBitmap(img, new Rectangle(ParseInt(bounds[2]), ParseInt(bounds[3]), img.Width, img.Height));
img.Save(stream, ImageFormat.Png);
browser.Dispose();
}
Response.Clear();
stream.WriteTo(Response.OutputStream);
stream.Dispose();
string fileName = "WidgetExport.png";
Response.ContentType = "application/octet-stream";
//System.ArgumentException throws here
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
Response.Flush();
}
です。
これは正しい方法ですか?上記のコードSystem.ArgumentExceptionのは、ヘッダ
での「Content-処分」を設定する際に数時間のために分析した後、あなたの提案