ArgumentOutOfRangeException使用BinaryWriter编写文件

时间:2010-10-21 07:19:16

标签: .net vb.net

我在客户端有函数获取文件,从中生成一个字节数组并将该数组发送到服务器上的Web服务! 这是客户端的功能:

Public Function GetFile(ByVal filename As String) As Byte()
    Dim binReader As New BinaryReader(File.Open(filename, FileMode.Open))
    binReader.BaseStream.Position = 0
    Dim binFile As Byte() = binReader.ReadBytes(Convert.ToInt32(binReader.BaseStream.Length))
    binReader.Close()
    Return binFile
End Function

现在服务器上的asmx webservice,获取发送的字节数组并将该字节数组转换为需要保存在服务器上的文件。这是webservice中需要执行此操作的功能:

   <WebMethod()> _
   Public Sub PutFile(ByVal buffer As Byte(), ByVal filename As String)
        Dim binWriter As New BinaryWriter(File.Create((filename), FileMode.CreateNew, FileAccess.ReadWrite))
        binWriter.Write(buffer)
        binWriter.Close()
    End Sub

在我调用此web服务后,我收到此错误:

Server was unable to process request. ---> System.ArgumentOutOfRangeException: Enum value was out of legal range. Parameter name: options    at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)

服务器上需要保存文件的路径是D:\ Archive 我设置了这个文件夹的权限,我给aspnet用户完全控制了这个文件夹,但即使在那之后我也得到同样的错误!

1 个答案:

答案 0 :(得分:0)

我可能会使用ReadAllBytesWriteAllBytes简化这两项功能:

Public Function GetFile(ByVal filename As String) As Byte()
    Return File.ReadAllBytes(filename)
End Function

并在服务器上:

<WebMethod()> _
Public Sub PutFile(ByVal buffer As Byte(), ByVal filename As String)
    File.WriteAllBytes(filename, buffer)
End Sub