# Can't upload file via C# application

**URL:** <https://www.ruby-forum.com/t/cant-upload-file-via-c-application/109514>\
**Category:** Rails\
**Created:** [July 16, 2007, 7:06am UTC](https://www.ruby-forum.com/t/cant-upload-file-via-c-application/109514 "2007-07-16T07:06:01Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kam\_kei\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/5f8ce5/32.png) [@Kam\_kei\_S](https://www.ruby-forum.com/u/Kam_kei_S)\
**Post date:** [July 16, 2007, 7:06am UTC](https://www.ruby-forum.com/t/cant-upload-file-via-c-application/109514/1 "2007-07-16T07:06:01Z")

</div>

When i try to upload a file via a C# application, i always get an error  
from ror.  
Here is the error  
Mon Jul 16 12:58:51 +0800 2007: Error calling Dispatcher.dispatch  
#\<EOFError: bad boundary end of body part\>  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/cgi\_multipart\_eof\_fix-2.1/lib/cgi\_multipart\_eof\_fix.rb:106:in  
`read_multipart' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/cgi_ext/raw_post_data_fix.rb:38:in `initialize\_query’  
C:/InstantRails/ruby/lib/ruby/1.8/cgi.rb:2275:in `initialize' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/cgi.rb:50:in `initialize’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/rails.rb:69:in  
`new' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/rails.rb:69:in `process’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:618:in  
`process_client' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:617:in `each’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:617:in  
`process_client' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:736:in `run’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:736:in  
`initialize' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:736:in `new’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:736:in  
`run' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:720:in `initialize’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:720:in  
`new' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel.rb:720:in `run’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/configurator.rb:271:in  
`run' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/configurator.rb:270:in `each’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/configurator.rb:270:in  
`run' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/bin/mongrel_rails:127:in `run’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/mongrel/command.rb:211:in  
`run' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/bin/mongrel_rails:243 C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:488:in `load’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active\_support/dependencies.rb:488:in  
`load' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:342:in `new\_constants\_in’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active\_support/dependencies.rb:488:in  
`load' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/servers/mongrel.rb:60 C:/InstantRails/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem\_original\_require’  
C:/InstantRails/ruby/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:27:in  
`require' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:495:in `require’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active\_support/dependencies.rb:342:in  
`new_constants_in' C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:495:in `require’  
C:/InstantRails/ruby/lib/ruby/gems/1.8/gems/rails-1.2.3/lib/commands/server.rb:39  
C:/InstantRails/ruby/lib/ruby/site\_ruby/1.8/rubygems/custom\_require.rb:27:in  
`gem_original_require' C:/InstantRails/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require’  
./script/server:3  
-e:4:in `load’  
-e:4

I don’t know what is going on.  
And here is my c# code  
using System;  
using System.Collections.Generic;  
using System.Text;  
using System.Web;  
using [System.Net](http://System.Net);  
using [System.IO](http://System.IO);

namespace ConsoleApplication13  
{  
class Program  
{  
static void Main(string[] args)  
{  
Program test = new Program();  
test.firstmethod();

```
    }
    private void firstmethod()
    {
        string url = "http://localhost:3002/filelink/upload2";
        string[] files = { "c:\\test.txt" };
        string logpath = null;
        UploadFilesToRemoteUrl(url, files, logpath);
    }
    private void UploadFilesToRemoteUrl(string url, string[] files,

```

string logpath)  
{

```
        long length = 0;
        string boundary = "----------------------------" +

```

DateTime.Now.Ticks.ToString(“x”);

```
        HttpWebRequest httpWebRequest2 =

```

(HttpWebRequest)WebRequest.Create(url);  
httpWebRequest2.ContentType = “multipart/form-data;  
boundary=” + boundary;  
httpWebRequest2.Method = “POST”;  
httpWebRequest2.KeepAlive = true;

```
        httpWebRequest2.Credentials =
        System.Net.CredentialCache.DefaultCredentials;

        Stream memStream = new System.IO.MemoryStream();

        byte[] boundarybytes2 =

```

System.Text.Encoding.ASCII.GetBytes(“–” + boundary + “\r\n”);  
byte[] boundarybytes =  
System.Text.Encoding.ASCII.GetBytes(“\r\n–” + boundary + “\r\n”);

```
        memStream.Write(boundarybytes2,0,boundarybytes2.Length);
        length += boundarybytes2.Length;

        string headerTemplate = "Content-Disposition: form-data;

```

name="{0}"; filename="{1}"\r\n Content-Type:  
application/octet-stream\r\n\r\n";

```
        for(int i=0;i<files.Length;i++)
        {

        string header =

```

string.Format(headerTemplate,“any\_file”,files[i]);

```
        byte[] headerbytes =

```

System.Text.Encoding.UTF8.GetBytes(header);

```
        memStream.Write(headerbytes,0,headerbytes.Length);
        length += headerbytes.Length;

        FileStream fileStream = new FileStream(files[i],

```

FileMode.Open, FileAccess.Read);  
byte[] buffer = new byte[1024];

```
        int bytesRead = 0;

        while ( (bytesRead = fileStream.Read(buffer, 0,

```

buffer.Length)) != 0 )  
{  
memStream.Write(buffer, 0, bytesRead);  
length += bytesRead;  
}

```
        memStream.Write(boundarybytes,0,boundarybytes.Length);
        length += boundarybytes.Length;

        fileStream.Close();
        }

        httpWebRequest2.ContentLength = memStream.Length;

        Stream requestStream = httpWebRequest2.GetRequestStream();

        memStream.Position = 0;
        byte[] tempBuffer = new byte[memStream.Length];
        memStream.Read(tempBuffer,0,tempBuffer.Length);
        memStream.Close();
        requestStream.Write(tempBuffer,0,tempBuffer.Length );
        requestStream.Close();

        WebResponse webResponse2 = httpWebRequest2.GetResponse();

        Stream stream2 = webResponse2.GetResponseStream();
        StreamReader reader2 = new StreamReader(stream2);

        Console.WriteLine(reader2.ReadToEnd());

        webResponse2.Close();
        httpWebRequest2 = null;
        webResponse2 = null;

        }
}

```

}

Here is My ruby on rails code  
class FilelinkController \< ApplicationController  
require “ftools”  
scaffold :filelink

def index  
redirect\_to :action =\> “list”  
end  
def list  
@filelink = Filelink.find\_all  
end

def uploads  
link = String.new()  
@link = Date.today().strftime(“%d\_%m\_%y”).to\_s()  
@filelink = Filelink.new()  
@filelink[‘patientname’] = @params[‘patientname’]  
@filelink[‘filelink’] = “#{RAILS\_ROOT}/public/file/”+@link+“.txt”  
File.open(@filelink[‘filelink’], “wb”) { |f|  
f.write(@params[‘any\_file’].read) }  
#do |f|  
#f.write(@params[‘any\_file’].read)  
#end  
@filelink.save  
redirect\_to :action =\> “list”  
end  
###The Used Part  
def upload2  
if File.size?(@params[‘any\_file’])\>0  
File.open(“#{RAILS\_ROOT}/public/file/try.txt”, “wb”) do |f|  
f.write(@params[‘any\_file’].read)  
end  
else  
redirect\_to :action =\>“list”  
end  
redirect\_to :action =\>“list”  
end

##### 

end

And here is the form

**Upload file form**
# **Add a patient records**

The HeartBeat file **:**

  

For more information, i use Aptana as the development platform of ror,  
and i use visual [studio.net](http://studio.net) 2005 as the c# development platform.
