WikiCrawler/Backend/MultipartUpload.cs
/// <summary>
/// Writes multi part HTTP POST request. Author : Farhan Ghumra
/// </summary>
Writes multi part HTTP POST request. Author : Farhan Ghumra
Writes multi part HTTP POST request . Author : Farhan Ghumra
private static void WriteMultipartForm(Stream s, string boundary, Dictionary<string, string> data,
		string fileName, string fileContentType, string fileKey,
		Action<Stream> writeFileData)
    {
        byte[] boundarybytes = Encoding.UTF8.GetBytes("--" + boundary + "\r\n");
        byte[] trailer = Encoding.UTF8.GetBytes("\r\n--" + boundary + "–-\r\n");
        string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
        string fileheaderTemplate = "Content-Disposition: file; name=\"{0}\"; filename=\"{1}\";\r\nContent-Type: {2}\r\n\r\n";
        bool bNeedsCRLF = false;
        if (data != null)
        {
            foreach (string key in data.Keys)
            {
                if (bNeedsCRLF)
                    WriteToStream(s, "\r\n");
                WriteToStream(s, boundarybytes);
                WriteToStream(s, string.Format(formdataTemplate, key, data[key]));
                bNeedsCRLF = true;
            }
        }
        if (bNeedsCRLF)
            WriteToStream(s, "\r\n");
        WriteToStream(s, boundarybytes);
        WriteToStream(s, string.Format(fileheaderTemplate, fileKey, fileName, fileContentType));
		writeFileData(s);
        WriteToStream(s, trailer);
    }
private static void WriteMultipartForm ( Stream s , string boundary , Dictionary < string , string > data , string fileName , string fileContentType , string fileKey , Action < Stream > writeFileData ) { byte [ ] boundarybytes = Encoding . UTF8 . GetBytes ( " " + boundary + " \r \n " ) ; byte [ ] trailer = Encoding . UTF8 . GetBytes ( " \r \n " + boundary + " \n ") ; string formdataTemplate = " \" \" \r \n \r \n " ; string fileheaderTemplate = " \" \" \" \" \r \n \r \n \r \n " ; bool bNeedsCRLF = false ; if ( data != null ) { foreach ( string key in data . Keys ) { if ( bNeedsCRLF ) WriteToStream ( s , " \r \n " ) ; WriteToStream ( s , boundarybytes ) ; WriteToStream ( s , string . Format ( formdataTemplate , key , data [ key ] ) ) ; bNeedsCRLF = true ; } } if ( bNeedsCRLF ) WriteToStream ( s , " \r \n " ) ; WriteToStream ( s , boundarybytes ) ; WriteToStream ( s , string . Format ( fileheaderTemplate , fileKey , fileName , fileContentType ) ) ; writeFileData ( s ) ; WriteToStream ( s , trailer ) ; }
Writes multi part HTTP POST request.
Writes multi part HTTP POST request .
// The first boundary // the last boundary. // the form data, properly formatted // the form-data file upload, properly formatted // Added to track if we need a CRLF or not. /// if we need to drop a CRLF, do that. /// Write the boundary. /// Write the key. // If we don't have keys, we don't need a crlf. // Write the file data to the stream.
s
Stream
boundary
string
data
Dictionary<string, string>
fileName
string
fileContentType
string
fileKey
string
writeFileData
Action<Stream>