【问题标题】:querystring missing value after postback回发后查询字符串缺失值
【发布时间】:2017-06-22 14:20:00
【问题描述】:

我正在使用 webcam.js 从使用 asp.net 网络表单应用程序的网络摄像头获取照片。问题是,我想根据“ID”查询字符串获取文件名。但是每次我点击“拍摄”按钮时,查询字符串的值都没有。

这是我的完整 HTML:

<%@ Page Language="vb" AutoEventWireup="false"  CodeBehind="Coba.aspx.vb" Inherits="myWebcam.Coba" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script src='<%=ResolveUrl("~/Plugin/jquery.webcam.js")%>' type="text/javascript"></script>
    <script type="text/javascript">
        var pageUrl = '<%=ResolveUrl("~/Coba.aspx")%>';
        $(function (){
           $("#Camera").webcam({
                 width: 320,
                 height: 240,
                 mode: "save",
                 swffile: '<%=ResolveUrl("Plugin/jscam.swf")%>',
                 onTick: function () { },
                 onSave: function () {
                 },
                 onCapture: function () {
                     webcam.save(pageUrl);
                 },
                 debug: function () { },
                 onLoad: function () { }
             });
        });

        function Capture() {

            webcam.capture();

            return false;
        };
     </script>
</head>
<body>
    <form id="form1" runat="server">

    <div>
    <h2>Index</h2>

    <input type="button" value="Shoot!" onclick="Capture()" />
    <div id="Camera"></div>
    </div>
    </form>
</body>
</html>

这是我的 asp.net 代码隐藏:

Imports System.IO
Imports System.Web.Services

Public Class Coba
    Inherits System.Web.UI.Page
    Public sID As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        sID = Request.QueryString("id")


        If Not Page.IsPostBack Then

            Capture(Trim(sID))
        End If
    End Sub
    Public Sub Capture(ByVal mID As String)
        Dim stream = Request.InputStream
        Dim dump As String

        If stream.Length > 0 Then

            Using reader = New StreamReader(stream)
                dump = reader.ReadToEnd()
            End Using

            Dim path = Server.MapPath("~/" & Trim(mID) & ".jpg")
            System.IO.File.WriteAllBytes(path, String_To_Bytes2(dump))
        End If
    End Sub

    Private Function String_To_Bytes2(strInput As String) As Byte()
        Dim numBytes As Integer = (strInput.Length) / 2
        Dim bytes As Byte() = New Byte(numBytes - 1) {}

        For x As Integer = 0 To numBytes - 1
            bytes(x) = Convert.ToByte(strInput.Substring(x * 2, 2), 16)
        Next

        Return bytes
    End Function
End Class

第一次运行页面时,我可以从查询字符串中获取“id”值。拍摄按钮触发回发并运行“Capture”子程序,但“id”查询字符串不返回任何内容。

这个问题有什么解决办法吗?

【问题讨论】:

  • 如果我正确阅读了您的代码,那么您不会为后续的POSTback 保留原始的queryString 数据。你有很多选项可以做到这一点 - 从设置 hidden 字段,使用 ViewState 等 - 所以你可以(再次)在 后续 请求中发送它(无论它是 @ 987654327@back 或GET)。

标签: asp.net webcam.js


【解决方案1】:

我尝试了代码,我认为存在错误概念。该插件似乎没有附加任何查询字符串。您的 ID 始终是 Nothing。我进行了调试,在QueryString中没有发现添加ID的痕迹。

文件名是您为自己分配逻辑的属性。 Here is a complete example in which the ID is not recovered.

即使在official page of the plugin 中,也没有通过查询字符串引用 ID。


其他方面,你不应该在界面和保存过程中使用同一个页面。页面加载总是被调用,你调用Capture(Trim(sID))即使在加载界面时。

我认为你必须改变这一行:

var pageUrl = '<%=ResolveUrl("~/Coba.aspx")%>';

var pageUrl = '<%=ResolveUrl("~/Coba_SaveImage.aspx")%>';

您所有的代码隐藏都在页面Coba_SaveImage.aspx 中。在不需要页面加载中,If Not Page.IsPostBack Then 始终是回发。

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-22
    • 1970-01-01
    • 1970-01-01
    • 2021-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多