【发布时间】:2014-04-10 05:45:36
【问题描述】:
我正在尝试分配隐藏字段值,如 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>
谁能指出我哪里出错了?
【问题讨论】:
-
您是否检查过它是否进入了 if 部分,因为您已经检查过会话是否可用,那么只有它将值分配给隐藏变量。另外你是否使用自动回发检查?
-
是的,因为它正在生成会话变量并将文件存储在其中,所以它显然应该具有正确的文件名?
-
是的,您是否通过调试检查了这些值是否被放入服务器端代码的隐藏字段中?有没有不放价值观的可能性。
-
看看我的回答。
标签: asp.net vb.net html-input