Regarding using Web service to handle file uploads

I am trying to code a Web Service in rails that can handle file uploads.
Now
as i read, SOAP 1.1 doesn’t support this yet…so i will have to use
SOAP4R,
right?

This is fine from server side…but will this API will be compatible to
.NET
clients, which will be using standard SOAP API perhaps.

Any idea, whats the way to go here?

hemant kumar wrote:

I am trying to code a Web Service in rails that can handle file uploads.

A file upload really isn’t that much different than accepting a field of
data as far as a web service is concerned…the only real consideration
is if you are accepting binary or ascii files for upload (as your
signature will change depending on the type you will accept/expect).

So basically you would just accept the data, check that it’s as expected
(allowing file uploads can be a major security risk, so PLEASE make sure
you put in good security checks on what you accpet)…then write it out
to the location you want it, and send back a message to the client with
status/results…

It shouldn’t really have much to do with the web service
specification…but if you need/want more details on the specific steps
to create something like this just let me know.

Kevin thanx for the response:

I thought so…and setout to use… :base64 to handle binary file
uploads…and my code is:

require ‘digest/sha1’
#require ‘soap/rpc/standaloneserver’
#require ‘soap/attachment’

class FileuploadApi < ActionWebService::API::Base
api_method :put_file,
:expects => [:string, :string, :base64],
:returns => [:int]
end

class FileuploadService < ActionWebService::Base
web_service_api FileuploadApi
before_invocation :try_authenticate
def put_file(name,password,file)
File.open(“temp.jpg”,“wb”) do |f|
f.write(file)
end
end

def try_authenticate(name,args)
password = Digest::SHA1.hexdigest(args[1].strip)
current_user = User.find(:first,
:conditions => [" name = ? and hashed_password = ?",
args[0].strip,password])
if current_user
return true
else
return false
end
end
end

first, i just want things to work…so i am not doing any
validations…but when i try to consume this web service from a Mono
app…i get:

Unhandled Exception: System.ArgumentException: Reading would overrun
buffer
in <0x001ab> System.IO.FileStream:Read (System.Byte[] dest, Int32
dest_offset, Int32 count)
in <0x00097> TestUpload.Test:Main (System.String[] args)

So, what are my options?

On 4/11/06, Kevin M. [email protected] wrote:

you put in good security checks on what you accpet)…then write it out


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


nothing much to talk

Kevin, looks like base64 only supports 8 bytes…so i make the call in a
loop…yeah its lousy method…besides i dont get any File info with
those 8 bytes…and somehow…some bits are added to the file recieved at
the server.

My modified is like:

require ‘digest/sha1’
#require ‘soap/rpc/standaloneserver’
#require ‘soap/attachment’

class FileuploadApi < ActionWebService::API::Base
api_method :put_file,
:expects => [:string, :string, :base64],
:returns => [:int]
end

class FileuploadService < ActionWebService::Base
web_service_api FileuploadApi
before_invocation :try_authenticate
def put_file(name,password,data)
File.open(“temp.png”,“a”) do |f|
f.write(data)
end
return 1
end

def try_authenticate(name,args)
password = Digest::SHA1.hexdigest(args[1].strip)
current_user = User.find(:first,
:conditions => [" name = ? and hashed_password = ?",
args[0].strip,password])
if current_user
return true
else
return false
end
end
end

And my .NET client is:
using System;
using System.IO;

namespace TestUpload
{
class Test
{
public static void Main(string[] args)
{
FileStream stream = new FileStream(“girl.png”,FileMode.Open);
FileuploadService fileService = new FileuploadService();
Byte[] data = new Byte[8];
while(stream.Read(data,0,8)!=0)
{
int result = fileService.PutFile(“hemant”,“hemant”,data);
if(result > 0)
Console.WriteLine(“Write Successful\n”);
}
}
}
}

I m really bugged by this…secure or not secure…i got to do
this…somehow…please reply you have a better solution.