UploadFileEx in IronPython

This is an example of a class in IronPython that can upload a file to a web site. This is a port from the example given in http://www.codeproject.com/KB/cs/uploadfileex.aspx, except that I did not consider the use of cookies...but the addition should be trivial.

Points to note are:

  1. the parameter query_string is of type NameValueCollection
  2. the parameter url must be a valid URI (e.g. www.somewebsite.com/somepage)
  3. the parameter content_type can be 'multipart/form-data'.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import System
from System import *
from System.Text import *
from System.IO import *
import clr
clr.AddReference("System.Xml")
from System.Xml import *
clr.AddReference("System.Web")
from System.Web import *
clr.AddReference("System.Net")
from System.Net import *
from System.Collections.Specialized import *

class FileUploader:
    @staticmethod
    def upload(upload_file,url,file_form_name,content_type,query_string):
        try:
            if file_form_name == None or file_form_name.Length == 0:
                file_form_name = 'file'
            if content_type == None or content_type.Length == 0:
                content_type = 'application/octet-stream'
            post_data = '?'
            if query_string != None:
                for key in query_string.Keys:
                    post_data += key + '=' + query_string.Get(key) + '&'
            uri = Uri(url + post_data)
            boundary = '----------' + System.DateTime.Now.Ticks.ToString('x')
            web_request = WebRequest.Create(uri)
            web_request.ContentType = 'multipart/form-data; boundary=' + \
                                        boundary
            web_request.Method = 'POST'
            sb = StringBuilder()
            sb.Append('--')
            sb.Append(boundary)
            sb.Append('\r\n')
            sb.Append('Content-Disposition: form-data; name="')
            sb.Append(file_form_name)
            sb.Append('"; filename="')
            sb.Append(Path.GetFileName(upload_file))
            sb.Append('"')
            sb.Append('\r\n')
            sb.Append('Content-Type: ')
            sb.Append(content_type)
            sb.Append('\r\n')
            sb.Append('\r\n')
            post_header = sb.ToString()
            post_header_bytes = Encoding.UTF8.GetBytes(post_header)
            boundary_bytes = Encoding.ASCII.GetBytes('\r\n--' + boundary + \
                                                        '\r\n')
            file_stream = FileStream(upload_file,FileMode.Open,FileAccess.Read)
            length = post_header_bytes.Length + file_stream.Length + \
                        boundary_bytes.Length
            web_request.ContentLength = length
            request_stream = web_request.GetRequestStream()
            request_stream.Write(post_header_bytes,0,post_header_bytes.Length)
            file_bytes = Encoding.UTF8.GetBytes(file_stream.ToString())
            while True:
                bytes_read = file_stream.Read(file_bytes, 0, file_bytes.Length)
                request_stream.Write(file_bytes, 0, bytes_read)
                if bytes_read == 0:
                    break
            request_stream.Write(boundary_bytes,0,boundary_bytes.Length)
            response = web_request.GetResponse()
            s = response.GetResponseStream()
            sr = StreamReader(s)
            return sr.ReadToEnd()
        except Exception,e:
            return e

Comments