资源解释为Document但使用MIME类型application / pdf进行传输

时间:2012-03-28 22:04:22

标签: c# javascript asp.net-mvc-3 mime-types

每当我触发下载PDF时,都会收到此警告。警告似乎没有任何负面影响,但我想知道为什么谷歌Chrome会通知我这个。我试过添加一个显式的响应头和内容长度,但无济于事。

export: function () {
    _stage.toDataURL(function (dataURL) {
        //POST the data because dataURL is too large to pass using a GET.
        $.post('../PlanView/GeneratePDF', { DataURL: dataURL }, function (imageURL) { location.href = imageURL; });
    });
}

[CompressionFilterAttribute]
public string GeneratePDF(string dataURL)
{
    dataURL = Regex.Replace(dataURL, "^data:image/(png|jpg);base64,", string.Empty);

    byte[] imageBytes = Convert.FromBase64String(dataURL);
    System.Drawing.Image image;
    using (MemoryStream memoryStream = new MemoryStream(imageBytes, 0, imageBytes.Length))
    {
        memoryStream.Write(imageBytes, 0, imageBytes.Length);
        image = System.Drawing.Image.FromStream(memoryStream, true);
    }

    Document document;
    float maxHeight;
    float maxWidth;
    if (image.Height > image.Width)
    {
        document = new Document(PageSize.A4);
        maxHeight = PageSize.A4.Height - (document.TopMargin + document.BottomMargin);
        maxWidth = PageSize.A4.Width - (document.LeftMargin + document.RightMargin);
    }
    else
    {
        document = new Document(PageSize.A4.Rotate());
        maxHeight = PageSize.A4.Width - (document.LeftMargin + document.RightMargin);
        maxWidth = PageSize.A4.Height - (document.TopMargin + document.BottomMargin);
    }

    using (MemoryStream memoryStream = new MemoryStream())
    {
        PdfWriter.GetInstance(document, memoryStream);
        document.Open();
        iTextSharp.text.Image planViewImage = iTextSharp.text.Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Png);

        //http://stackoverflow.com/questions/1106339/resize-image-to-fit-in-bounding-box
        //Find the scaling factor by determining which is smaller: MaxWidth/w or MaxHeight/h, then multiply w and h by that number.
        float scale = (maxHeight / image.Height < maxWidth / image.Width) ? (maxHeight / image.Height) : (maxWidth / image.Width);
        planViewImage.ScalePercent(scale * 100);

        document.Add(planViewImage);
        document.Close();

        //Store the image in a buffer because its hard to download an image during POST.
        Session["PlanViewPDF"] = memoryStream.ToArray();
    }

    //Return URL to another MVC URL which can GET the image.
    return "../PlanView/ExportPDF";
}

[CompressionFilterAttribute]
public ActionResult ExportPDF()
{
    byte[] buffer = Session["PlanViewPDF"] as byte[];
    Response.Headers.Add("Content-Type", "application/pdf");
    Response.AddHeader("Content-Length", buffer.Length.ToString());

    return File(buffer, "application/pdf", string.Format("Plan View {0:yyyy-MM-dd_hh-mm-ss-tt}.pdf", DateTime.Now)); 
}

enter image description here

0 个答案:

没有答案