在DLL方法
 '文件上传
        Public Function fileup()
            '//定义初始设置
            Dim filesavepath As String = "upload"             '文件保存目录
            Dim newname As String                          '根据日期生成新文件名
            newname = DateTime.Now.Year.ToString() & DateTime.Now.Month.ToString() & DateTime.Now.Day.ToString() & DateTime.Now.Minute.ToString() & DateTime.Now.Second.ToString()
            Dim fileextension As String                    '文件类型
            Dim filetype As String = ".rar,.zip,.doc"       '文件上传类型定义
            Dim maxfile As Integer = 1024 * 3000        '图像上传大小3000K
            Dim msg As String
            Dim files As HttpFileCollection = System.Web.HttpContext.Current.Request.Files
            Dim fileoldname As String
            Dim ifile As Integer
            Dim upfile As HttpPostedFile
            upfile = files(ifile)
            fileoldname = System.IO.Path.GetFileName(upfile.FileName)
            '文件开始上传
            If fileoldname <> "" Then
                Try
                    '文件类型检查
                    fileextension = System.IO.Path.GetExtension(upfile.FileName).ToLower()
                    If (filetype.IndexOf(fileextension) = -1) Then
                        msg = -1
                    Else
                        '文件大小检查
                        If (upfile.ContentLength > maxfile) Then
                            msg = 0
                        Else
                            '保存文件
                            upfile.SaveAs(Server.MapPath(filesavepath & "/" & newname & fileextension))
                            Session("upfile") = newname
                            msg = 1
                        End If
                    End If
                Catch
                    msg = 2
                End Try
            End If
            Return msg
        End Function
前台调用的时候
  Sub fileup(ByVal sender As System.Object, ByVal e As EventArgs)
        If upfile.PostedFile.ContentLength > 0 Then
            Select Case db.fileup
                Case 1
                    upfile_res.Text = "上传成功"
                    file.Visible = False
                Case -1
                    upfile_res.Text = "请检查文件类型是否允许上传"
                Case 0
                    upfile_res.Text = "文件大小超过限制!"
                Case 2
                    upfile_res.Text = "上传出现异常!请重新尝试!"
            End Select
        End If
    End Sub
出现的情况的如果我先传图像,然后传附件就可以正常,但是如果我只传附件就老提示我文件大小超过限制,但是我查询过返回的msg值,居然是空的,那是为什么呢?很着急,请大家多帮助了

解决方案 »

  1.   

    asp.net默认对传送文件的大小有限制,图片小所以可以上传you can do like this in web.config:
    <httpRuntime maxRequestLength="500000" />或者你可以分割文件,那样比较复杂
      

  2.   

    同意楼上,
    你上传的RAR文件有多大?是否超过默认值了?
      

  3.   

    Dim maxfile As Integer = 1024 * 3000
    As Integer ?这应该用整型吗?
      

  4.   

    在web.config中这样写了
      <system.web>
       <!-- 文件上传大小设置 
        -->
          <httpRuntime maxRequestLength="400000"
              executionTimeout="200"
                 />
    在HTML页码是
    <TR class="tr1">
    <TD align="center">上传图像</TD>
    <TD>
    <asp:Panel id="img" Runat="server"><INPUT class="tr1" id="upimg" type="file" size="32" runat="server" width="80%">&nbsp; 
    <asp:Button id="up_img" onclick="addimg" runat="server" Text="上传" BorderStyle="Groove"></asp:Button>
    </asp:Panel><asp:Label id="upimg_res" runat="server"></asp:Label></TD>
    </TR>
    <TR class="tr2">
    <TD align="center">上传附件</TD>
    <TD >
    <asp:Panel Width="60%" id="file" Runat="server"><INPUT class="tr2" id="upfile" type="file" size="32" runat="server" width="80%">&nbsp; 
    <asp:Button id="up_file" onclick="fileup" runat="server" Text="上传" BorderStyle="Groove"></asp:Button>
    </asp:Panel><asp:Label id="upfile_res" runat="server"></asp:Label></TD>
    </TR>
      

  5.   

    我上传的文件改为10K也不可以的,我测试过因为它每次都返回的是文件大小的限制提示,于是我把DLL中fileup方法的upfile.ContentLength返回来看,但是每次都是为0了。于是我想会不是在postfile后,要清空的问题了于是我在代码页面加上
    If upfile.PostedFile.ContentLength > 0 Then
                Select Case db.fileup
                    Case 1
                        upfile_res.Text = "上传成功"
                        file.Visible = False
                    Case -1
                        upfile_res.Text = "请检查文件类型是否允许上传"
                    Case 0
                        upfile_res.Text = "文件大小超过限制!"
                    Case 2
                        upfile_res.Text = "上传出现异常!请重新尝试!"
                End Select
    '添加这个
    upfile. upfile.Dispose()
            End If
    同样在上传图像的代码中也写了这个上去还是一样的,~~
    天~~~~~~~~~~~为什么~~~~~~~~~~
      

  6.   

    db.fileup是
    在dll名称空间中的一个方法
    dim db as new myweb.config
    ‘调用方法
    db.fileup
      

  7.   

    文件我测试的是10K啊,而且我在web.config中配置了的啊