使用vb上传文件并同时提交表单

时间:2011-11-25 22:10:48

标签: asp.net html vb.net forms

所以我在网页上做两件事有点小问题。我正在使用a将文件上传到我的Web服务器,同时我还有其他用来从用户那里获取数据(名字,姓氏等)表格是runat = server,应该上传的按钮该文件(在初步检查文件正常,并且表单字段填写正确后)也是runat服务器,点击onserver。

主要问题是,当我给表单一个“get”方法时,我可以得到所需的结果,将我的所有数据放在我想要的url中,但是我无法上传文件。如果我删除该标签,我可以上传文件,但我的网址中没有数据。

(相关代码)

    <script language="VB" runat="server">
        Dim str As String
        Sub Button1_Click(ByVal Source As Object, ByVal e As EventArgs)

            'Dim submitFunction As String = "<script type='text/javascript'> function submitform() { document.myform.submit();}" & "</" & "script>"
            Session("firstTime") = 4
            Session("errCheck") = 1
            If InputF.Value = "" Then
                Span1.InnerHtml = "Error: you must enter a file name"
                Return
            End If

            If Not (InputF.PostedFile Is Nothing) Then
                Try
                    If Session("firstTime") <> 1 Then

                        If Request.QueryString("fName") = "" Then
                            str += "Please Enter your first name <br/>"
                            Session("errCheck") += 1
                        End If
                        If Request.QueryString("lName") = "" Then
                            str += "Please Enter your last name <br />"
                            Session("errCheck") += 1
                        End If
                        If Request.QueryString("addr1") = "" Then
                            str += "Please Enter your address <br />"
                            Session("errCheck") += 1
                        End If
                        If Request.QueryString("city") = "" Then
                            str += "Please Enter a city name <br />"
                            Session("errCheck") += 1
                        End If
                        If Len(Request.QueryString("prov")) <> 2 Then
                            str += "Please Enter a 2 character province <br />"
                            Session("errCheck") += 1
                        End If
                        If Len(Request.QueryString("pCode")) <> 6 Then
                            str += "Please Enter a valid postal code <br />"
                            Session("errCheck") += 1
                        End If
                        If Request.QueryString("hPhone") = "" Then
                            str += "Please Enter your home phone <br />"
                            Session("errCheck") += 1
                        ElseIf Len(Request.QueryString("hPhone")) <> 10 Then
                            str += "Please enter a 10 digit number for your home phone <br />"
                            Session("errCheck") += 1
                        End If
                    End If
                    If Session("errCheck") = 1 Then
                        InputF.PostedFile.SaveAs(("FILE PATH TO MY SERVER" & Request.QueryString("compName") & " - " & Request.QueryString("fName") & ", " & Request.QueryString("lName") & InputF.Value))
                        'Response.Redirect("default.aspx?fName=" & Request.QueryString("fName"))
                    End If

                Catch exc As Exception
                    str += "Error Saving File"
                    Span1.InnerHtml = "Error saving file"
                    Session("errCheck") += 1
                End Try

            End If

        End Sub 'Button1_Click 

</script>


                    <form  name="myform" id="myform" method="get" runat="server" enctype="multipart/form-data">
                    <table>
                        <h2><%  If Session("errCheck") <> 1 Then
                                    Response.Write(Str)
                                End If%></h2>
                        <tr>
                            <td align="right"><label>Resume/Documents</label></td>
                            <td><input type="file" runat="server" id="InputF" name="InputF" onchange="handleFiles(this.files)" /><input type="button" id="adder" value="add another document" onclick="addInput('dynamicInput');" /></td>
                            <div id="dynamicInput">
                            </div>
                        </tr>
                        <tr>
                            <td>                
                                <span id=Span1 
                                style="font: 8pt verdana;" 
                                runat="server" />
                            </td>
                        </tr>
                    <input type="hidden" id="Hidden13" name="compName" value="<% response.Write(request.QueryString("compName")) %>" />
                    <input type="hidden" id="posCode" name="posCode" value="<% response.Write(request.QueryString("posCode"))%>" />
                    <input type="hidden" id="reqNum" name="reqNum" value="<% response.Write(request.QueryString("reqNum")) %>" />
                    <input type="hidden" id="company" name="company" value="<% response.Write(request.QueryString("company"))%>" />
                    <input type="hidden" id="division" name="division" value="<% response.Write(request.QueryString("division")) %>" />
                    <input type="hidden" id="department" name="department" value="<% response.Write(request.QueryString("department"))%>" />
                        <tr>
                            <td align="right">First name<span style="color:Red;">*</span>:</td>
                            <td><input type='text' name='fName' id='fName'  size='50'  maxlength="50"value="<% Response.Write(Request.QueryString("fName"))%>" /></td>
                        </tr>
                        <tr>
                            <td align="right"><label>Last name </label><span style="color:Red;">*</span>:</td>
                            <td><input type='text' name='lName' id='lName'  size='50'  maxlength="50" value="<% response.Write(request.QueryString("lName"))%>"/></td>
                        </tr>
                        <tr>
                            <td align="right"><label>Initial</label>:</td>
                            <td><input type='text' name='init' id='init'  size='2'  maxlength="2" value="<% response.Write(request.QueryString("init"))%>"/></td>
                        </tr>
                        <tr>
                            <td align="right"><label>Email</label>:</td>
                            <td><input type='text' name='email' id='email'  size='50' maxlength="50" value="<% response.Write(request.QueryString("email"))%>"/></td>
                        </tr>
                        <tr>
                            <td align="right"><label>Address 1</label><span style="color:Red;">*</span>:</td>
                            <td><input type='text' name='addr1' id='addr1'  size='25' maxlength="25" value="<% response.Write(request.QueryString("addr1"))%>"/></td>
                        </tr>
                        <tr>
                            <td align="right"><label>Address 2</label>:</td>
                            <td><input type='text' name='addr2' id='addr2'  size='25' maxlength="25" value="<% response.Write(request.QueryString("addr2"))%>"/></td>
                        </tr>
                        <tr>
                            <td align="right"><label>City</label><span style="color:Red;">*</span>:</td>
                            <td><input type='text' name='city' id='city'  size='25' maxlength="25" value="<% response.Write(request.QueryString("city"))%>"/></td>
                        </tr>
                        <br />
                        <tr>
                            <td align="right"><label>Province</label><span style="color:Red;">*</span>:</td>
                            <td><input type='text' name='prov' id='prov'  size='2' maxlength="2" value="<% response.Write(request.QueryString("prov"))%>"/></td>
                        </tr>
                        <tr>
                            <td align="right"><label>Country</label>:</td>
                            <td><input type='text' name='count' id='count'  size='25' maxlength="25" value="<% response.Write(request.QueryString("count"))%>"/></td>
                        </tr>
                        <tr>
                            <td align="right"><label>Postal code</label><span style="color:Red;">*</span>:</td>
                            <td><input type='text' name='pCode' id='pCode'  size='25' maxlength="25" value="<% response.Write(request.QueryString("pCode"))%>"/></td>
                        </tr>
                        <tr>
                            <td align="right"><label>Home Phone</label><span style="color:Red;">*</span>:</td>
                            <td><input type='text' name='hPhone' id='hPhone'  size='15' maxlength="15" value="<% response.Write(request.QueryString("hPhone"))%>"/></td>
                            <p>Format for phone numbers: Areacode, phone number, no spaces, no dashes ie: 2041231234</P>
                        </tr>
                        <tr>
                            <td align="right"><label>Work Phone</label>:</td>
                            <td><input type='text' name='wPhone' id='wPhone'  size='25' maxlength="25" value="<% response.Write(request.QueryString("wPhone"))%>"/></td>
                        </tr>
                    </table>
                           <input type=button 
                            id="Button1" 
                            value="Submit Application" 
                            OnServerClick="Button1_Click" 
                            runat="server" />
                </form>

1 个答案:

答案 0 :(得分:0)

我自己也接近了。在表单提交时,我对其他文件进行了所有验证,然后如果全部检出,我将表单提交到不同的页面(使用其他不相关的代码)。

基本上我发布的代码很好,我只是在提交时遗漏了一些东西

简而言之:

'if there are no errors with the user feilds
If Session("errCheck") = 1 Then
                    Try
                    If Not (System.IO.Path.GetFileName(InputF.PostedFile.FileName.ToString()) Is Nothing) Then
                        Try
                            InputF.PostedFile.SaveAs(("somepath:\somepath\somepath\somepath\" & Trim(Request.Form("compName")) & " - " & Request.Form("lName") & ", " & Request.Form("fName") & "-" & System.IO.Path.GetFileName(InputF.PostedFile.FileName.ToString())))
                        Catch ex As Exception
                            Session("errCheck") += 1
                            str += "Error Saving File" & ex.Message
                        End Try
If Session("errCheck") = 1 Then
                        Response.Redirect("final2.aspx?fName=" & Request.Form("fName") & "&lName=" & Request.Form("lName") & "&compName=" & Trim(Request.Form("compName")) &
                        "&posCode=" & Request.Form("posCode") & "&reqNum=" & Request.Form("reqNum") & "&company=" & Request.Form("company") & 
                        "&division=" & Request.Form("division") & "&department=" & Request.Form("department") & "&init=" & Request.Form("init") & 
                        "&email=" & Request.Form("email") & "&addr1=" & Trim(Request.Form("addr1")) & "&addr2=" & Request.Form("addr2") & 
                        "&city=" & Request.Form("city") & "&prov=" & Request.Form("prov") & "&count=" & Request.Form("count") & 
                        "&pCode=" & Request.Form("pCode") & "&hPhone=" & Request.Form("hPhone") & "&wPhone=" & Request.Form("wPhone"))
                    End If

希望这对任何看我的问题,现在是我的答案的人都有意义。