开发者

How to print multiple files with js and asp.net

I had to run some server side code to open multiple files via javascript something to this effect:

Assume r is just a reader with some data:

If r IsNot Nothing Then
    Dim sb As New StringBuilder()
    Do While r.Read()
        'added below to replace \\ with http://
        strURL = Replace(CType(r("AttachmentLink"), String), "\\myServer\MyFolder\MyPath", "http://MyFolder/MyPath", , , CompareMethod.Text)
        'added below to replace \ with /
        strURL = Replace(strURL, "\", "/")
        sb.AppendLine("window.open('" & strURL & "', '_blank', 'menubar=no');")
    Loop
    ClientScript.RegisterStartupScript(Me.GetType(), "popup", sb.ToString(), True)
End If

This works great for opening multiple attachments... but now I need to not only open them up but print them...

So I tried just taking what I had above and modifying a little:

Do While r.Read()
    'added below to replace \\ with http://
    strURL = Replace(CType(r("AttachmentLink"), String), "\\myServer\MyFolder\MyPath", "http://MyFolder/MyPath", , , CompareMethod.Text)
    'added below to replace \ with /
    strURL = Replace(strURL, "\", "/")
    sb.AppendLine("var oWindow = window.open('" &开发者_StackOverflow社区; strURL & "', '_blank', 'menubar=no');")
    sb.AppendLine("oWindow.print();")
    sb.AppendLine("oWindow.close();")
Loop
ClientScript.RegisterStartupScript(Me.GetType(), "popup", sb.ToString(), True)

This of course does not work, no error but nothing comes up. I was hoping to get each window open up and a print dialog from javascript to pop up...

Any ideas?


Due to browser javascript security reasons you're not allowed to print a child window.

What you could do is add a load script into those pages you'd like to print that will initiate printing from that child window instead.

Check this answer that shows a bit different aproach in a sense that:

  1. it creates an iframe
  2. loads some content in it and also
  3. adds onload event to print it...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜