Since WebClient's uploadData doesn't encode data, then what will be the effect of adding a “Content-Type”, “multipart/form-data” header to it

匆匆过客 提交于 2019-11-29 10:58:58

Why don't you use UploadFile of WebClient over https if you want it to be secure? and that will automatically take care of adding multipart/form-data.

Example using UploadFile

http://msdn.microsoft.com/en-us/library/36s52zhs.aspx

And one more thing, encoding and encrypting are 2 different things.

Edit:

You should probably tag your question as Silverlight if you you are using WebClient in your WebClient project. Anyways, WebClient class in SL doesn't have any UploadData method. See this for more info:

http://msdn.microsoft.com/en-us/library/system.net.webclient%28v=vs.95%29.aspx

Anyways, here is the working solution to your problem:

In your button click, have this code:

OpenFileDialog dialog = new OpenFileDialog();
            bool? retVal = dialog.ShowDialog();
            if (retVal.HasValue && retVal == true)
            {
                using (Stream stream = dialog.File.OpenRead())
                {
                    MemoryStream memoryStream = new MemoryStream();
                    stream.CopyTo(memoryStream);
                    WebClient webClient = new WebClient();
                    webClient.Headers["Content-type"] = "multipart/form-data; boundary=---------------------------" + _boundaryNo;
                    webClient.OpenWriteAsync(new Uri("http://localhost:1463/Home/File", UriKind.Absolute), "POST", new { Stream = memoryStream, FileName = dialog.File.Name });
                    webClient.OpenWriteCompleted += new OpenWriteCompletedEventHandler(webClient_OpenWriteCompleted);
                }
            } 

and the event itself:

void webClient_OpenWriteCompleted(object sender, OpenWriteCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                Stream responseStream = e.Result as Stream;                
                dynamic obj = e.UserState;
                MemoryStream memoryStream = obj.Stream as MemoryStream;
                string fileName = obj.FileName;
                if (responseStream != null && memoryStream != null)
                {
                    string headerTemplate = string.Format("-----------------------------{0}\r\n", _boundaryNo);

                    memoryStream.Position = 0;
                    byte[] byteArr = memoryStream.ToArray();
                    string data = headerTemplate + string.Format("Content-Disposition: form-data; name=\"pic\"; filename=\"{0}\"\r\nContent-Type: application/octet-stream\r\n\r\n", fileName);
                    byte[] header = Encoding.UTF8.GetBytes(data);
                    responseStream.Write(header, 0, header.Length);

                    responseStream.Write(byteArr, 0, byteArr.Length);
                    header = Encoding.UTF8.GetBytes("\r\n");
                    responseStream.Write(byteArr, 0, byteArr.Length);

                    byte[] trailer = System.Text.Encoding.UTF8.GetBytes(string.Format("-----------------------------{0}--\r\n", _boundaryNo));
                    responseStream.Write(trailer, 0, trailer.Length);                    
                }
                memoryStream.Close();
                responseStream.Close();
            }
        }

where _boundaryNo is private string _boundaryNo = DateTime.Now.Ticks.ToString("x");

I had it working with Asp.Net MVC 4 and Silverlight 5.

Good luck :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!