我正在尝试分配隐藏字段值,如page_load所示:
If Session("tempDir") Is Nothing Then
If Request.Files.Count > 0 Then
Dim tempDir As String
tempDir = Path.GetRandomFileName()
tempDir = tempDir.Substring(0, tempDir.LastIndexOf("."))
IO.Directory.CreateDirectory(Server.MapPath("~/Uploads/" & tempDir))
IO.Directory.CreateDirectory(Server.MapPath("~/Downloads/" & tempDir))
Session.Add("tempDir", tempDir)
currentDirectory.Value = Session("tempDir").ToString
Dim chunk As Integer = If(Context.Request("chunk") IsNot Nothing, Integer.Parse(Context.Request("chunk")), 0)
Dim fileName As String = If(Context.Request("name") IsNot Nothing, Context.Request("name"), String.Empty)
myfilename = fileName
Session.Add("filename", myfilename)
finalfilename.Value = currentDirectory.Value & fileName
finalfilename.Value = Session("filename").ToString
MsgBox(finalfilename.Value)
workingDir.Value = Server.MapPath("~/Uploads/" & tempDir)
Session.Add("tempDir", workingDir.Value)
MsgBox(workingDir.Value)
waitFlag.Value = "True"
Session.Add("flag", waitFlag.Value)
waitFlag.Value = Session("flag").ToString
MsgBox(waitFlag.Value)
Dim fileUpload As HttpPostedFile = Context.Request.Files(0)
Dim uploadPath = Context.Server.MapPath("~/uploads/" & tempDir)
Using fs = New FileStream(Path.Combine(uploadPath, fileName), If(chunk = 0, FileMode.Create, FileMode.Append))
Dim buffer = New Byte(fileUpload.InputStream.Length - 1) {}
fileUpload.InputStream.Read(buffer, 0, buffer.Length)
fs.Write(buffer, 0, buffer.Length)
End Using
End If
End If
这些是我的html输入隐藏字段:
<input type="hidden" id="workingDir" runat="server"/>
<input type="hidden" id="finalfilename" runat="server"/>
<input type="hidden" id="waitFlag" runat="server"/>
<input id="currentDirectory" type="hidden" runat="server"/>
这是我的ajax调用,我将所有隐藏的字段值视为未定义:
<script type="text/javascript">
$('#btnconvert').click(function () {
var wd = $('#workingDir').val();
alert(wd);
var fn = $('#finalfilename').val();
alert(fn);
var bf = $('#waitFlag').val();
alert(bf);
$.ajax({
type: "POST",
url: "Default.aspx/ProcessFiles",
data: "{'workingDir':'" + wd + "', 'finalfilename':'" + fn + "', 'waitFlag':'" + bf + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert("success");
},
error: function (data) {
alert("fail");
}
});
});
</script>
任何人都可以指出我哪里出错了吗?
答案 0 :(得分:1)
你犯了一些小错误。
问题在于您的Jquery按钮单击事件,您可以从隐藏字段中检索值。您隐藏的字段是服务器端控件。因此,当它被渲染时,隐藏字段的id将不仅仅是 workingDir,finalfilename,waitFlag
所以你应该使用 ClientID 来获取隐藏字段中的值,如下所述:
var wd = $('#<%=workingDir.ClientID%>').val();
alert(wd);
var fn = $('#<%=finalfilename.ClientID%>').val();
alert(fn);
var bf = $('#<%=waitFlag.ClientID%>').val();
alert(bf);
您的JQUERY将如下所示:
<script type="text/javascript">
$('#btnconvert').click(function () {
var wd = $('#<%=workingDir.ClientID%>').val();
alert(wd);
var fn = $('#<%=finalfilename.ClientID%>').val();
alert(fn);
var bf = $('#<%=waitFlag.ClientID%>').val();
alert(bf);
$.ajax({
type: "POST",
url: "Default.aspx/ProcessFiles",
data: "{'workingDir':'" + wd + "', 'finalfilename':'" + fn + "', 'waitFlag':'" + bf + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert("success");
},
error: function (data) {
alert("fail");
}
});
});
</script>