using UnityEngine;
using System.Collections;
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;

public class NewBehaviourScript : MonoBehaviour {

    string url = "http://219.85.250.149:8080/UploadFile"; // 伺服器上傳網址  
    string filePath = "/Users/Loli/Desktop/Easy Download/23-121026161100.jpg"; // 檔案路徑

    void Start () {
        UploadFilesToRemoteUrl (urlfilePath);
    }

    private string UploadFilesToRemoteUrl(string urlstring file)
    {
        // Create a boundry
        string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
        
        // Create the web request
        HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
        httpWebRequest.ContentType = "multipart/form-data; boundary=" + boundary;
        httpWebRequest.Method = "POST";
        httpWebRequest.KeepAlive = true;
        httpWebRequest.AllowWriteStreamBuffering = false;
        
        httpWebRequest.Credentials =
            System.Net.CredentialCache.DefaultCredentials;
        
        // Get the boundry in bytes
        byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
        
        // Get the header for the file upload
        string headerTemplate = "Content-Disposition: form-data; name=\"{0}\";filename=\"{1}\"\r\n Content-Type: application/octet-stream\r\n\r\n";
        
        // Add the filename to the header
        string header = string.Format(headerTemplate"file"file);
        
        //convert the header to a byte array
        byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
        
        // Add all of the content up.
        httpWebRequest.ContentLength = new FileInfo(file).Length + headerbytes.Length + (boundarybytes.Length * 2) + 2;
        
        // Get the output stream
        Stream requestStream = httpWebRequest.GetRequestStream();
        
        // Write out the starting boundry
        requestStream.Write(boundarybytes0boundarybytes.Length);
        
        // Write the header including the filename.
        requestStream.Write(headerbytes0headerbytes.Length);
        
        // Open up a filestream.
        FileStream fileStream = new FileStream(fileFileMode.OpenFileAccess.Read);
        
        // Use 4096 for the buffer
        byte[] buffer = new byte[4096];
        
        int bytesRead = 0;
        // Loop through whole file uploading parts in a stream.
        while ((bytesRead = fileStream.Read(buffer0buffer.Length)) != 0)
        {
            requestStream.Write(buffer0bytesRead);
            requestStream.Flush();
        }
        
        boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
        
        // Write out the trailing boundry
        requestStream.Write(boundarybytes0boundarybytes.Length);
        
        // Close the request and file stream
        requestStream.Close();
        fileStream.Close();
        
        WebResponse webResponse = httpWebRequest.GetResponse();
        
        Stream responseStream = webResponse.GetResponseStream();
        StreamReader responseReader = new StreamReader(responseStream);
        
        string responseString = responseReader.ReadToEnd();
        
        // Close response object.
        webResponse.Close();
        
        return responseString;
    }

}

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 黃彥霖 的頭像
    黃彥霖

    彥霖 實驗筆記

    黃彥霖 發表在 痞客邦 留言(1) 人氣()