WebService(VB)如何接收传输过来的文件
我在服务里建立了
其中第一个参数是传递过来的byte,第二个是参数是文件 。请问第一个参数的写法是否正确
<WebMethod()> _
Public Function UploadFile(ByVal fs(), ByVal FileName) As String
Dim m As MemoryStream
’下面不知道怎么写了,如何把fs赋值给m
Dim f As FileStream
f = New FileStream(Server.MapPath(".") & "\" & FileName, FileMode.Create)
m.WriteTo(f)
f.Close()
Return "文件上传成功"
End Function
问题点数:20、回复次数:3Top
1 楼czcty(peter)回复于 2005-08-24 11:33:49 得分 0
搞定了:
upload.asmx.vb
========================================================
Imports System.IO
Imports System.Web.Services
<System.Web.Services.WebService(Namespace := "http://tempuri.org/WebServiceAds/Upload")> _
Public Class Upload
Inherits System.Web.Services.WebService
#Region " Web 服务设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Web 服务设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加您自己的初始化代码
End Sub
'Web 服务设计器所必需的
Private components As System.ComponentModel.IContainer
'注意: 以下过程是 Web 服务设计器所必需的
'可以使用 Web 服务设计器修改此过程。
'不要使用代码编辑器修改它。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
components = New System.ComponentModel.Container()
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
'CODEGEN: 此过程是 Web 服务设计器所必需的
'不要使用代码编辑器修改它。
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
#End Region
' Web 服务示例
' HelloWorld() 示例服务返回字符串 Hello World。
' 若要生成项目,请取消注释以下行,然后保存并生成项目。
' 若要测试此 Web 服务,请确保 .asmx 文件是起始页
' 并按 F5 键。
'
'<WebMethod()> _
'Public Function HelloWorld() As String
' Return "Hello World"
'End Function
<WebMethod()> _
Public Function UploadFile(ByVal fs() As Byte, ByVal FileName As String) As String
Dim m As MemoryStream
m = New MemoryStream(fs)
Dim f As FileStream
f = New FileStream(Server.MapPath(".") & "\upload\" & FileName, FileMode.Create)
m.WriteTo(f)
f.Close()
Return "文件上传成功"
End Function
End Class
upload.aspx
============================================================
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="upload.aspx.vb" Inherits="WebServiceAds.upload1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>upload</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体"><INPUT id="File1" style="Z-INDEX: 101; LEFT: 48px; POSITION: absolute; TOP: 88px" type="file"
name="File1" runat="server">
<asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 320px; POSITION: absolute; TOP: 88px" runat="server"
Text="Button" Width="88px"></asp:Button>
<asp:TextBox id="TextBox6" style="Z-INDEX: 103; LEFT: 56px; POSITION: absolute; TOP: 32px" runat="server"
Width="168px" Height="32px"></asp:TextBox></FONT>
</form>
</body>
</HTML>
upload.aspx.vb
===============================================
Public Class upload1
Inherits System.Web.UI.Page
#Region " Web 窗体设计器生成的代码 "
'该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents File1 As System.Web.UI.HtmlControls.HtmlInputFile
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents TextBox6 As System.Web.UI.WebControls.TextBox
'注意: 以下占位符声明是 Web 窗体设计器所必需的。
'不要删除或移动它。
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If File1.PostedFile.ContentLength > 0 Then
Dim uploadcp As New WebReferenceUpFile.Upload
'文件和路径的分割数组
Dim FileSplit() As String = Split(File1.PostedFile.FileName, "\")
'文件名
Dim FileName As String = FileSplit(FileSplit.Length - 1)
'File1.PostedFile.SaveAs(Server.MapPath(".") & "\upload\" & FileName)
'''
Dim fs As System.IO.Stream
fs = File1.PostedFile.InputStream
Dim b() As Byte
ReDim b(File1.PostedFile.ContentLength)
fs.Read(b, 0, File1.PostedFile.ContentLength)
' Response.Write(o.UploadFile(b, FileName));
TextBox6.Text = (uploadcp.UploadFile(b, FileName))
fs.Close()
'''
Else
TextBox6.Text = "too small"
End If
End Sub
End Class
Top
2 楼zhsu(不懂生活的人)回复于 2005-08-25 14:03:20 得分 20
最好用异步传输机制这样容易出现阻塞。Top
3 楼czcty(peter)回复于 2005-08-25 15:29:55 得分 0
请指教。Top




