| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

「Anzai」の版間の差分

提供: MyMemoWiki
ナビゲーションに移動 検索に移動
(ページの作成:「Metro XAML Rating Control www.perpetuumsoft.com Star Tech Corner Star Tech Corner TUESDAY, MARCH 03, 2009 Silverlight Multi-part File Upload Form Post…」)
 
 
(同じ利用者による、間の4版が非表示)
1行目: 1行目:
Metro XAML Rating Control www.perpetuumsoft.com
+
Metro XAML [[R]]ating Control www.perpetuumsoft.com
  
 
Star Tech Corner Star Tech Corner
 
Star Tech Corner Star Tech Corner
5行目: 5行目:
  
  
TUESDAY, MARCH 03, 2009
+
TUESDAY, MA[[R]]CH 03, 2009
  
  
  
Silverlight Multi-part File Upload Form Post
+
[[Silverlight]] Multi-part File Upload Form Post
  
Silverlight not actually very web friendly. It lacks of built-in support for the current HTML form post protocol. So, we have to write it our own. I have written an extension class and two serializer classes, one for normal form post (DataContractQueryStringSerializer) while the other for multipart upload form post (DataContractMultiPartSerializer).
+
[[Silverlight]] not actually very web friendly. It lacks of built-in support for the current HTML form post protocol. So, we have to write it our own. I have written an extension class and two serializer classes, one for normal form post (DataContractQueryStringSerializer) while the other for multipart upload form post (DataContractMultiPartSerializer).
  
 
If you only want to have the working code, just copy the code below. Detail explanation is available at Multi-Part Form Post in Shane's Shelf
 
If you only want to have the working code, just copy the code below. Detail explanation is available at Multi-Part Form Post in Shane's Shelf
17行目: 17行目:
 
public static class Extensions
 
public static class Extensions
  
public static void PostFormAsync(this HttpWebRequest request, object parameters, AsyncCallback callback) { request.Method = "POST"; request.ContentType = "applicatio n/x-www-form-urlencoded"; request.BeginGetRequestStream(new AsyncCallback(asyncResult => { Stream stream = request.EndGe tRequestStream(asyncResult); DataContractQueryStringSerial izer ser = new DataContractQueryStringSeria lizer(); ser.WriteObject(stream, param eters); stream.Close(); request.BeginGetResponse(call back, request); }), request); }
+
public static void PostFormAsync(this HttpWeb[[R]]equest request, object parameters, AsyncCallback callback) { request.Method = "POST"; request.ContentType = "applicatio n/x-www-form-urlencoded"; request.BeginGet[[R]]equestStream(new AsyncCallback(async[[R]]esult => { Stream stream = request.EndGe t[[R]]equestStream(async[[R]]esult); DataContractQueryStringSerial izer ser = new DataContractQueryStringSeria lizer(); ser.WriteObject(stream, param eters); stream.Close(); request.BeginGet[[R]]esponse(call back, request); }), request); }
  
public static void PostMultiPartAsync (this HttpWebRequest request, object parame ters, AsyncCallback callback) { request.Method = "POST"; string boundary = "---------------" + DateTime.Now.Ticks.ToString(); request.ContentType = "multipart/form-data; boundary=" + boundary; request.BeginGetRequestStream(new AsyncCallback(asyncResult => { Stream stream = request.EndGe tRequestStream(asyncResult);
+
public static void PostMultiPartAsync (this HttpWeb[[R]]equest request, object parame ters, AsyncCallback callback) { request.Method = "POST"; string boundary = "---------------" + DateTime.Now.Ticks.ToString(); request.ContentType = "multipart/form-data; boundary=" + boundary; request.BeginGet[[R]]equestStream(new AsyncCallback(async[[R]]esult => { Stream stream = request.EndGe t[[R]]equestStream(async[[R]]esult);
  
DataContractMultiPartSerializ er ser = new DataContractMultiPartSerialize r(boundary); ser.WriteObject(stream, param eters); stream.Close(); request.BeginGetResponse(call back, request); }), request); }
+
DataContractMultiPartSerializ er ser = new DataContractMultiPartSerialize r(boundary); ser.WriteObject(stream, param eters); stream.Close(); request.BeginGet[[R]]esponse(call back, request); }), request); }
  
 
public class DataContractQueryStringSeria lizer
 
public class DataContractQueryStringSeria lizer
  
public void WriteObject(Stream stream , object data) { StreamWriter writer = new StreamW riter(stream); if (data != null) { if (data is Dictionary<string , string>) { foreach (var entry in dat a as Dictionary<string, string>) { writer.Write("{0}={1} &", entry.Key, entry.Value); } } else { foreach (var prop in data .GetType().GetFields()) { foreach (var attribut e in prop.GetCustomAttributes(true)) { if (attribute is DataMemberAttribute) {
+
public void WriteObject(Stream stream , object data) { StreamWriter writer = new StreamW riter(stream); if (data != null) { if (data is Dictionary&lt;string , string&gt;) { foreach (var entry in dat a as Dictionary&lt;string, string&gt;) { writer.Write("{0}={1} &", entry.Key, entry.Value); } } else { foreach (var prop in data .GetType().GetFields()) { foreach (var attribut e in prop.GetCustomAttributes(true)) { if (attribute is DataMemberAttribute) {
  
 
ribute member = attribute as DataMemberAttr ibute;
 
ribute member = attribute as DataMemberAttr ibute;
  
<blockquote>0}={1}&", member.Name ?? prop.Name, prop. GetValue(data)); } } } foreach (var prop in data .GetType().GetProperties()) { if (prop.CanRead) { foreach (var attr ibute in prop.GetCustomAttributes(true)) {</blockquote>
+
&lt;blockquote&gt;0}={1}&", member.Name ?? prop.Name, prop. GetValue(data)); } } } foreach (var prop in data .GetType().GetProperties()) { if (prop.Can[[R]]ead) { foreach (var attr ibute in prop.GetCustomAttributes(true)) {&lt;/blockquote&gt;
  
 
is DataMemberAttribute)
 
is DataMemberAttribute)
45行目: 45行目:
 
private void WriteEntry(StreamWriter writer, string key, object value) { if (value != null) { writer.Write("--"); writer.WriteLine(boundary); if (value is FileInfo) {
 
private void WriteEntry(StreamWriter writer, string key, object value) { if (value != null) { writer.Write("--"); writer.WriteLine(boundary); if (value is FileInfo) {
  
FileInfo f = value as Fil eInfo; writer.WriteLine(@"Conten t-Disposition: form-data; name=""{0}""; fil ename=""{1}""", key, f.Name); writer.WriteLine("Content -Type: application/octet-stream"); writer.WriteLine("Content -Length: " + f.Length); writer.WriteLine(); writer.Flush(); Stream output = writer.Ba seStream; Stream input = f.OpenRead
+
FileInfo f = value as Fil eInfo; writer.WriteLine(@"Conten t-Disposition: form-data; name=""{0}""; fil ename=""{1}""", key, f.Name); writer.WriteLine("Content -Type: application/octet-stream"); writer.WriteLine("Content -Length: " + f.Length); writer.WriteLine(); writer.Flush(); Stream output = writer.Ba seStream; Stream input = f.Open[[R]]ead
  
byte[] buffer = new byte[ 4096]; for (int size = input.Rea d(buffer, 0, buffer.Length); size > 0; size = input.Read(buffer, 0, buffer.Length)) { output.Write(buffer, 0, size); } output.Flush(); writer.WriteLine(); } else { writer.WriteLine(@"Conten t-Disposition: form-data; name=""{0}""", ke
+
byte[] buffer = new byte[ 4096]; for (int size = input.Rea d(buffer, 0, buffer.Length); size &gt; 0; size = input.Read(buffer, 0, buffer.Length)) { output.Write(buffer, 0, size); } output.Flush(); writer.WriteLine(); } else { writer.WriteLine(@"Conten t-Disposition: form-data; name=""{0}""", ke
  
 
writer.WriteLine(); writer.WriteLine(value.To String()); } } }
 
writer.WriteLine(); writer.WriteLine(value.To String()); } } }
  
public void WriteObject(Stream stream , object data) { StreamWriter writer = new StreamW riter(stream); if (data != null) { if (data is Dictionary<string , object>) { foreach (var entry in dat a as Dictionary<string, object>) { WriteEntry(writer, en try.Key, entry.Value); } } else { foreach (var prop in data .GetType().GetFields()) { foreach (var attribut e in prop.GetCustomAttributes(true)) { if (attribute is DataMemberAttribute) {
+
public void WriteObject(Stream stream , object data) { StreamWriter writer = new StreamW riter(stream); if (data != null) { if (data is Dictionary&lt;string , object&gt;) { foreach (var entry in dat a as Dictionary&lt;string, object&gt;) { WriteEntry(writer, en try.Key, entry.Value); } } else { foreach (var prop in data .GetType().GetFields()) { foreach (var attribut e in prop.GetCustomAttributes(true)) { if (attribute is DataMemberAttribute) {
  
 
ribute member = attribute as DataMemberAttr ibute;
 
ribute member = attribute as DataMemberAttr ibute;
  
iter, member.Name ?? prop.Name, prop.GetVal ue(data)); } } } foreach (var prop in data .GetType().GetProperties()) { if (prop.CanRead) { foreach (var attr ibute in prop.GetCustomAttributes(true)) {
+
iter, member.Name ?? prop.Name, prop.GetVal ue(data)); } } } foreach (var prop in data .GetType().GetProperties()) { if (prop.Can[[R]]ead) { foreach (var attr ibute in prop.GetCustomAttributes(true)) {
  
 
is DataMemberAttribute)
 
is DataMemberAttribute)
65行目: 65行目:
 
} } } } } writer.Write("--"); writer.Write(boundary); writer.WriteLine("--"); writer.Flush(); }
 
} } } } } writer.Write("--"); writer.Write(boundary); writer.WriteLine("--"); writer.Flush(); }
  
The usage is as follows: First a PHP file
+
The usage is as follows: First a [[PHP]] file
  
<?php print_r($_REQUEST); $src = $_FILES['y']['tmp_name']; $dest = "C:\\Windows\\Temp\\".$_FILES['y'][ 'name']; echo $src; echo "\r\n"; echo $dest; echo @copy($src, $dest);
+
&lt;?php print_r($_REQUEST); $src = $_FILES['y']['tmp_name']; $dest = "C:\\Windows\\Temp\\".$_FILES['y'][ 'name']; echo $src; echo "\r\n"; echo $dest; echo @copy($src, $dest);
  
 
Then the Page control
 
Then the Page control
73行目: 73行目:
 
public partial class Page : UserControl
 
public partial class Page : UserControl
  
public Page() { InitializeComponent(); // Create a request object HttpWebRequest request = (HttpWeb Request)WebRequest.Create(new Uri("http://l ocalhost/rms/test.php")); OpenFileDialog dlg = new OpenFile Dialog(); if (dlg.ShowDialog().Value) { request.PostMultiPartAsync(ne w Dictionary<string, object> { { "x", "1" } , { "y", dlg.File } }, new AsyncCallback(as yncResult => { HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(a syncResult);
+
public Page() { InitializeComponent(); // Create a request object HttpWeb[[R]]equest request = (HttpWeb [[R]]equest)Web[[R]]equest.Create(new Uri("http://l ocalhost/rms/test.php")); OpenFileDialog dlg = new OpenFile Dialog(); if (dlg.ShowDialog().Value) { request.PostMultiPartAsync(ne w Dictionary&lt;string, object&gt; { { "x", "1" } , { "y", dlg.File } }, new AsyncCallback(as ync[[R]]esult =&gt; { HttpWeb[[R]]esponse response = (HttpWeb[[R]]esponse)request.EndGet[[R]]esponse(a sync[[R]]esult);
  
Stream responseStream = r esponse.GetResponseStream(); StreamReader reader = new StreamReader(responseStream); this.Dispatcher.BeginInvo ke(delegate { // output is a TextBl
+
Stream responseStream = r esponse.Get[[R]]esponseStream(); Stream[[R]]eader reader = new Stream[[R]]eader(responseStream); this.Dispatcher.BeginInvo ke(delegate { // output is a TextBl
  
output.Text = reader. ReadToEnd(); response.Close(); }); })); } }
+
output.Text = reader. [[R]]eadToEnd(); response.Close(); }); })); } }
  
 
Since it is able to serialize data contract, you could actually replace
 
Since it is able to serialize data contract, you could actually replace
  
new Dictionary<string, object> { { "x", "1" }, { "y", dlg.File } }
+
new Dictionary&lt;string, object&gt; { { "x", "1" }, { "y", dlg.File } }
  
 
new Point(){X=1, Y=2}
 
new Point(){X=1, Y=2}
87行目: 87行目:
 
given the point class is like this:
 
given the point class is like this:
  
[DataContract] public class Point
+
[[DataContract]] |  public class Point
  
[DataMember] public int X { get; set; } [DataMember(Name="y")] public int Y { get; set; }
+
[[DataMember]] |  public int X { get; set; } [[DataMember(Name="y")]] |  public int Y { get; set; }
  
  
105行目: 105行目:
 
Cool! The multipart handling was exactly what I needed. Nice work!
 
Cool! The multipart handling was exactly what I needed. Nice work!
  
Reply
+
[[R]]eply
  
 
Anonymous 4:53 PM
 
Anonymous 4:53 PM
  
Hi, I failed in EndGetResponse(), and got security exception. What will the encoding data would like? Could you give me an example?
+
Hi, I failed in EndGet[[R]]esponse(), and got security exception. What will the encoding data would like? Could you give me an example?
  
 
Thanks a lot~
 
Thanks a lot~
  
Reply
+
[[R]]eply
  
 
Naveen 7:10 PM
 
Naveen 7:10 PM
  
Could you post the receiving part? Meaning, how to take the multipart request in the server (Jersey) and process the multipart request?
+
Could you post the recei[[vi]]ng part? Meaning, how to take the multipart request in the server (Jersey) and process the multipart request?
  
Reply
+
[[R]]eply
  
 
Shane Ng 1:13 AM
 
Shane Ng 1:13 AM
125行目: 125行目:
 
It's just a multi-part form
 
It's just a multi-part form
  
standard Java Web Container (e.g. Tomcat) will be able to process it natively.
+
standard Java Web Container (e.g. [[Tomcat]]) will be able to process it natively.
  
Reply
+
[[R]]eply
  
  
135行目: 135行目:
 
Comment as: Select profile...
 
Comment as: Select profile...
  
  Preview
+
  Pre[[vi]]ew
  
  

2020年2月16日 (日) 04:22時点における最新版

Metro XAML Rating Control www.perpetuumsoft.com

Star Tech Corner Star Tech Corner


TUESDAY, MARCH 03, 2009


Silverlight Multi-part File Upload Form Post

Silverlight not actually very web friendly. It lacks of built-in support for the current HTML form post protocol. So, we have to write it our own. I have written an extension class and two serializer classes, one for normal form post (DataContractQueryStringSerializer) while the other for multipart upload form post (DataContractMultiPartSerializer).

If you only want to have the working code, just copy the code below. Detail explanation is available at Multi-Part Form Post in Shane's Shelf

public static class Extensions

public static void PostFormAsync(this HttpWebRequest request, object parameters, AsyncCallback callback) { request.Method = "POST"; request.ContentType = "applicatio n/x-www-form-urlencoded"; request.BeginGetRequestStream(new AsyncCallback(asyncResult => { Stream stream = request.EndGe tRequestStream(asyncResult); DataContractQueryStringSerial izer ser = new DataContractQueryStringSeria lizer(); ser.WriteObject(stream, param eters); stream.Close(); request.BeginGetResponse(call back, request); }), request); }

public static void PostMultiPartAsync (this HttpWebRequest request, object parame ters, AsyncCallback callback) { request.Method = "POST"; string boundary = "---------------" + DateTime.Now.Ticks.ToString(); request.ContentType = "multipart/form-data; boundary=" + boundary; request.BeginGetRequestStream(new AsyncCallback(asyncResult => { Stream stream = request.EndGe tRequestStream(asyncResult);

DataContractMultiPartSerializ er ser = new DataContractMultiPartSerialize r(boundary); ser.WriteObject(stream, param eters); stream.Close(); request.BeginGetResponse(call back, request); }), request); }

public class DataContractQueryStringSeria lizer

public void WriteObject(Stream stream , object data) { StreamWriter writer = new StreamW riter(stream); if (data != null) { if (data is Dictionary<string , string>) { foreach (var entry in dat a as Dictionary<string, string>) { writer.Write("{0}={1} &", entry.Key, entry.Value); } } else { foreach (var prop in data .GetType().GetFields()) { foreach (var attribut e in prop.GetCustomAttributes(true)) { if (attribute is DataMemberAttribute) {

ribute member = attribute as DataMemberAttr ibute;

<blockquote>0}={1}&", member.Name ?? prop.Name, prop. GetValue(data)); } } } foreach (var prop in data .GetType().GetProperties()) { if (prop.CanRead) { foreach (var attr ibute in prop.GetCustomAttributes(true)) {</blockquote>

is DataMemberAttribute)

rAttribute member = attribute as DataMember Attribute;

ite("{0}={1}&", member.Name ?? prop.Name, p rop.GetValue(data, null));

} } } } writer.Flush(); } }

public class DataContractMultiPartSeriali

private string boundary; public DataContractMultiPartSerialize r(string boundary) { this.boundary = boundary; }

private void WriteEntry(StreamWriter writer, string key, object value) { if (value != null) { writer.Write("--"); writer.WriteLine(boundary); if (value is FileInfo) {

FileInfo f = value as Fil eInfo; writer.WriteLine(@"Conten t-Disposition: form-data; name=""{0}""; fil ename=""{1}""", key, f.Name); writer.WriteLine("Content -Type: application/octet-stream"); writer.WriteLine("Content -Length: " + f.Length); writer.WriteLine(); writer.Flush(); Stream output = writer.Ba seStream; Stream input = f.OpenRead

byte[] buffer = new byte[ 4096]; for (int size = input.Rea d(buffer, 0, buffer.Length); size > 0; size = input.Read(buffer, 0, buffer.Length)) { output.Write(buffer, 0, size); } output.Flush(); writer.WriteLine(); } else { writer.WriteLine(@"Conten t-Disposition: form-data; name=""{0}""", ke

writer.WriteLine(); writer.WriteLine(value.To String()); } } }

public void WriteObject(Stream stream , object data) { StreamWriter writer = new StreamW riter(stream); if (data != null) { if (data is Dictionary<string , object>) { foreach (var entry in dat a as Dictionary<string, object>) { WriteEntry(writer, en try.Key, entry.Value); } } else { foreach (var prop in data .GetType().GetFields()) { foreach (var attribut e in prop.GetCustomAttributes(true)) { if (attribute is DataMemberAttribute) {

ribute member = attribute as DataMemberAttr ibute;

iter, member.Name ?? prop.Name, prop.GetVal ue(data)); } } } foreach (var prop in data .GetType().GetProperties()) { if (prop.CanRead) { foreach (var attr ibute in prop.GetCustomAttributes(true)) {

is DataMemberAttribute)

rAttribute member = attribute as DataMember Attribute;

y(writer, member.Name ?? prop.Name, prop.Ge tValue(data, null));

} } } } } writer.Write("--"); writer.Write(boundary); writer.WriteLine("--"); writer.Flush(); }

The usage is as follows: First a PHP file

<?php print_r($_REQUEST); $src = $_FILES['y']['tmp_name']; $dest = "C:\\Windows\\Temp\\".$_FILES['y'][ 'name']; echo $src; echo "\r\n"; echo $dest; echo @copy($src, $dest);

Then the Page control

public partial class Page : UserControl

public Page() { InitializeComponent(); // Create a request object HttpWebRequest request = (HttpWeb Request)WebRequest.Create(new Uri("http://l ocalhost/rms/test.php")); OpenFileDialog dlg = new OpenFile Dialog(); if (dlg.ShowDialog().Value) { request.PostMultiPartAsync(ne w Dictionary<string, object> { { "x", "1" } , { "y", dlg.File } }, new AsyncCallback(as yncResult => { HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(a syncResult);

Stream responseStream = r esponse.GetResponseStream(); StreamReader reader = new StreamReader(responseStream); this.Dispatcher.BeginInvo ke(delegate { // output is a TextBl

output.Text = reader. ReadToEnd(); response.Close(); }); })); } }

Since it is able to serialize data contract, you could actually replace

new Dictionary<string, object> { { "x", "1" }, { "y", dlg.File } }

new Point(){X=1, Y=2}

given the point class is like this:

DataContract | public class Point

DataMember | public int X { get; set; } DataMember(Name="y") | public int Y { get; set; }


Shane Ng at 2:21 AM

Share 0


4 comments:

Dai Lo 3:28 AM

Cool! The multipart handling was exactly what I needed. Nice work!

Reply

Anonymous 4:53 PM

Hi, I failed in EndGetResponse(), and got security exception. What will the encoding data would like? Could you give me an example?

Thanks a lot~

Reply

Naveen 7:10 PM

Could you post the receiving part? Meaning, how to take the multipart request in the server (Jersey) and process the multipart request?

Reply

Shane Ng 1:13 AM

It's just a multi-part form

standard Java Web Container (e.g. Tomcat) will be able to process it natively.

Reply


Enter your comment...

Comment as: Select profile...

Preview


Publish

› Home

View web version


ABOUT ME


Shane Ng

Enjoy your life, enjoy programming. View my complete profile


Powered by Blogger