Vamos ver como podemos enviar arquivos por webservices.
WebServices
C#
using System.ComponentModel;
using System.Web.Services;
namespace WebService2
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class Service1 : WebService
{
[WebMethod]
public string RecebeArquivo(byte[] arquivoData)
{
return "Arquivo Recebido com Sucesso com " + arquivoData.Length + " bytes";
}
}
}
VB.NET
Imports System.Web.Services
Imports System.ComponentModel
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding (ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class Service1
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function RecebeArquivo(ByVal arquivoData As Byte()) As String
Return "Arquivo Recebido com Sucesso com " + arquivoData.Length.ToString() + " bytes"
End Function
End Class
Após criar o WebServices, adicionamos a WebReference ao Projeto. No exemplo atual, o nome que demos foi “TransfereArquivoCSharp” para o exemplo em C#, e “TransfereArquivVB” para o exemplo em VB.Net
Código para Acessar o WebServices
C#
using System.IO;
using ConsoleApplication23.TransfereArquivoCSharp;
namespace ConsoleApplication23
{
internal class Program
{
private static void Main(string[] args)
{
// Cria Objetos
byte[] conteudoBinarioDoArquivo;
Service1 webServices;
FileStream stream;
// Inicializa o WebServices
webServices = new Service1();
// Carrega um Arquivo
using (stream = File.OpenRead("c:\\tmp\\MeuArquivo.tmp"))
{
conteudoBinarioDoArquivo = new byte[stream.Length];
stream.Read(conteudoBinarioDoArquivo, 0, conteudoBinarioDoArquivo.Length);
stream.Close();
}
// Envia o Arquivo
string resposta = webServices.RecebeArquivo(conteudoBinarioDoArquivo);
}
}
}
VB.NET
Imports ConsoleApplication24.TransfereArquivVB
Imports System.IO
Module Module1
Sub Main()
' Cria Objetos
Dim conteudoBinarioDoArquivo As Byte()
Dim webServices As Service1
Dim stream As FileStream
' Inicializa o WebServices
webServices = New Service1()
'Carrega um Arquivo
stream = File.OpenRead("c:\tmp\MeuArquivo.tmp")
Using (stream)
ReDim conteudoBinarioDoArquivo(stream.Length)
stream.Read(conteudoBinarioDoArquivo, 0, conteudoBinarioDoArquivo.Length)
stream.Close()
End Using
' Envia o Arquivo
Dim resposta As String = webServices.RecebeArquivo(conteudoBinarioDoArquivo)
End Sub
End Module
Se tudo ocorreu como planejado, a variável “resposta” deverá conter a mensagem de recebimento do arquivo e o tamanha do mesmo.
Espero ter ajudado.
Até o próximo…
Posts Relacionados:



Be the first to start a conversation