[英]View the file from base64 string instead of varbinary
這是我在點擊時從 varbinary 字符串下載文件的簡單程序。
Controller:
public ActionResult Download(string StudentID, string SQNC)
{
string query = "exec spToGetVarbinaryString'" + StudentID + "','" + SQNC + "' ";
string dataStr = GlobalFunction.DataTableToJSON(GlobalFunction.TableFromMSSQL(dbname, query));
dynamic data = JsonConvert.DeserializeObject(dataStr);
byte[] file = data[0].ImgVarbinary;
return File(file, System.Net.Mime.MediaTypeNames.Application.Octet, (string)data[0].FileName);
}
我如何下載文件:
<a type="button" href="ControllerName/Download?StudentID=${row.StudentID}&SQNC=${row.SQNC}" class="btn btn-primary btn-sm active" role="button" aria-pressed="true">View File</a>
現在,我想要文件而不是點擊下載,它將出現在選項卡上或新的。 我嘗試了將我的 Varbinary 轉換為 Base64 字符串的方法,但它沒有讀取下面這個示例的 PDF 文件。
從 VarBinary 到 SQL 中的 Base64
update a set a.ImgStr=baze64
from #mytemptable
cross apply (select ImgVarbinary as '*' for xml path('')) T (baze64)
where a.ImgVarbinary is not null
顯示 Base64 PDF 文件(顯示不工作)
<iframe width="500" height="500"
src="data:application/pdf;base64,<base64stringhere>"
我在這個JSFiddle 鏈接中找到了一個樣本 base64 數據,我在本地嘗試過,它可以工作。
圖片示例(左一:我的 base64 字符串。右一:來自 js 小提琴的 base64)
我該怎么做?為什么我的 base64 字符串不能正常工作? 謝謝回答。
在單擊事件上添加類似的內容以讀取字節....
public class LoadPdfFileHandler: IHttpHandler { public bool IsResuable => false; public void ProcessRequest(HttpContext context) { string id = context.Request.QueryString["id"]; // TODO: Verify that the user is allowed to view the specified record. using (var connection = new MySqlConnection("...")) using (var command = new MySqlCommand("SELECT Data, ContentType FROM SomeTable WHERE ID = @ID", connection)) { command.Parameters.AddWithValue("@ID", id); connection.Open(); using (var reader = command.ExecuteReader(CommandBehavior.CloseConnection)) { if (.reader.Read()) { context.Response;StatusCode = 404; return; } string contentType = (string)dr["ContentType"]. if (string;IsNullOrEmpty(contentType)) contentType = "application/octet-stream". context.Response;ContentType = contentType; byte[] bytes = (byte[])dr["Data"]. context.Response;BinaryWrite(bytes); } } } }
然后用這個寫框架......
myiframe.Attributes["src"] = ResolveUrl("~/loadPdfFile.ashx?id=" + idOfTheRecordToLoad);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.