# Regarding using Web service to handle file uploads

**URL:** <https://www.ruby-forum.com/t/regarding-using-web-service-to-handle-file-uploads/93807>\
**Category:** Rails\
**Created:** [April 11, 2006, 12:05am UTC](https://www.ruby-forum.com/t/regarding-using-web-service-to-handle-file-uploads/93807 "2006-04-11T00:05:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hemant\_kumar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/46a35a/32.png) [@hemant\_kumar](https://www.ruby-forum.com/u/hemant_kumar)\
**Post date:** [April 11, 2006, 12:05am UTC](https://www.ruby-forum.com/t/regarding-using-web-service-to-handle-file-uploads/93807/1 "2006-04-11T00:05:33Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![hemant\_kumar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/46a35a/32.png) [@hemant\_kumar](https://www.ruby-forum.com/u/hemant_kumar)\
**Post date:** [April 11, 2006, 4:34pm UTC](https://www.ruby-forum.com/t/regarding-using-web-service-to-handle-file-uploads/93807/2 "2006-04-11T16:34:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![hemant\_kumar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/46a35a/32.png) [@hemant\_kumar](https://www.ruby-forum.com/u/hemant_kumar)\
**Post date:** [April 11, 2006, 9:36pm UTC](https://www.ruby-forum.com/t/regarding-using-web-service-to-handle-file-uploads/93807/3 "2006-04-11T21:36:39Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> you put in good security checks on what you accpet)…then write it out
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
nothing much to talk

---

<div class="post-metadata">

**Author:** ![hemant\_kumar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/46a35a/32.png) [@hemant\_kumar](https://www.ruby-forum.com/u/hemant_kumar)\
**Post date:** [April 11, 2006, 11:44pm UTC](https://www.ruby-forum.com/t/regarding-using-web-service-to-handle-file-uploads/93807/4 "2006-04-11T23:44:17Z")

</div>

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](http://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.
