如果你需要跨域上传内容到另外一个域名并且需要获取返回值,使用Asp.Net的作为代理是最好的办法,要是客户端直接提交到iframe中,由于跨域是无法用javascript获取到iframe中返回的内容的。此时需要在自己的网站做一个动态页作为代理,将表单提交到动态页,动态页负责将表单的内容使用WebClient或HttpWebRequest将表单数据再上传到远程服务器,由于在服务器端进行操作,就不存在跨域问题了。
WebClient上传只包含键值对的文本信息示例代码:
string uriString = "http://localhost/login.aspx";
// 创建一个新的 WebClient 实例.
WebClient myWebClient = new WebClient();
string postData = "Username=admin&Password=admin";
// 注意这种拼字符串的ContentType
myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded");
// 转化成二进制数组
byte[] byteArray = Encoding.ASCII.GetBytes(postData);
// 上传数据,并获取返回的二进制数据.
byte[] responseArray = myWebClient.UploadData(uriString,"POST",byteArray);
</div>
WebClient上传只包含文件的示例代码:
String uriString = "http://localhost/uploadFile.aspx";
// 创建一个新的 WebClient 实例.
WebClient myWebClient = new WebClient();
string fileName = @"C:/upload.txt";
// 直接上传,并获取返回的二进制数据.
byte[] responseArray = myWebClient.UploadFile(uriString,"POST",fileName);
</div>
对于既包含文件又包含文本键值对信息的示例代码,需要构造表单提交的内容,对于学asp的同学来说,下面的表单提交内容一定不会陌生
-----------------------------7d429871607fe
Content-Disposition: form-data; name="file1"; filename="G:/homepage.txt"
Content-Type: text/plain
:http://www.weikejianghu.com
-----------------------------7d429871607fe
Content-Disposition: form-data; name="filename"
default filename
-----------------------------7d429871607fe--</div>
所以只要拼一个这样的byte[] data数据Post过去,就可以达到同样的效果了。但是一定要注意,对于这种带有文件上传的,其ContentType是不一样的,例如上面的这种,其ContentType为"multipart/form-data; boundary=---------------------------7d429871607fe"。有了ContentType,我们就可以知道boundary(就是上面的"---------------------------7d429871607fe"),知道boundary了我们就可以构造出我们所需要的byte[] data了,最后,不要忘记,把我们构造的ContentType传到WebClient中(例如:webClient.Headers.Add("Content-Type", ContentType);)这样,就可以通过WebClient.UploadData 方法上载文件数据了。
using System; using System.Web; using System.IO; using System.Net; using System.Text; using System.Collections; namespace UploadData.Common { public class CreateBytes { Encoding encoding = Encoding.UTF8; public byte[] JoinBytes(ArrayList byteArrays) { int length = 0; int readLength = 0; // 加上结束边界 string endBoundary = Boundary + "-- "; byte[] endBoundaryBytes = encoding.GetBytes(endBoundary); byteArrays.Add(endBoundaryBytes); foreach (byte[] b in byteArrays) { length += b.Length; } byte[] bytes = new byte[length]; // 遍历复制 foreach (byte[] b in byteArrays) { b.CopyTo(bytes, readLength); readLength += b.Length; } return bytes; } public bool UploadData(string uploadUrl, byte[] bytes, out byte[] responseBytes) { WebClient webClient = new WebClient(); webClient.Headers.Add("Content-Type", ContentType); try { responseBytes = webClient.UploadData(uploadUrl, bytes); return true; } catch (WebException ex) { Stream resp = ex.Response.GetResponseStream(); responseBytes = new byte[ex.Response.ContentLength]; resp.Read(responseBytes, 0, responseBytes.Length); } return false; } /// 获取普通表单区域二进制数组 public byte[] CreateFieldData(string fieldName, string fieldValue) { string textTemplate = Boundary + " Content-Disposition: form-data; name="{0}" {1} "; string text = String.Format(textTemplate, fieldName, fieldValue); byte[] bytes = encoding.GetBytes(text); return bytes; } public byte[] CreateFieldData(string fieldName, string filename, string contentType, byte[] fileBytes) { string end = " "; string textTemplate = Boundary + " Content-Disposition: form-data; name="{0}"; filename="{1}" Content-Type: {2} "; // 头数据 string data = String.Format(textTemplate, fieldName, filename, contentType); byte[] bytes = encoding.GetBytes(data); // 尾数据 byte[] endBytes = encoding.GetBytes(end); // 合成后的数组 byte[] fieldData = new byte[bytes.Length + fileBytes.Length + endBytes.Length]; bytes.CopyTo(fieldData, 0); // 头数据 fileBytes.CopyTo(fieldData, bytes.Length); // 文件的二进制数据 endBytes.CopyTo(fieldData, bytes.Length + fileBytes.Length); // return fieldData; } public string Boundary { get { string[] bArray, ctArray; string contentType = ContentType; ctArray = contentType.Split(';'); if (ctArray[0].Trim().ToLower() == "multipart/form-data") { bArray = ctArray[1].Split('='); return "--" + bArray[1]; } return null; } } public string ContentType { get { if (HttpContext.Current == null) { return "multipart/form-data; boundary=---------------------------7d5b915500cee"; } return HttpContext.Current.Request.ContentType; } } } } using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using UploadData.Common; using System.IO; namespace UploadDataWin { public class frmUpload : System.Windows.Forms.Form { private System.Windows.Forms.Label lblAmigoToken; private System.Windows.Forms.TextBox txtAmigoToken; private System.Windows.Forms.Label lblFilename; private System.Windows.Forms.TextBox txtFilename; private System.Windows.Forms.Button btnBrowse; private System.Windows.Forms.TextBox txtFileData; private System.Windows.Forms.Label lblFileData; private System.Windows.Forms.Button btnUpload; private System.Windows.Forms.OpenFileDialog openFileDialog1; private System.Windows.Forms.TextBox txtResponse; private System.ComponentModel.Container components = null; public frmUpload() { InitializeComponent(); } protected override void Dispose(bool disposing) { if (disposing) { if (components != null) { components.Dispose(); } } base.Dispose(disposing); } private void InitializeComponent() { this.lblAmigoToken = new System.Windows.Forms.Label(); this.txtAmigoToken = new System.Windows.Forms.TextBox(); this.lblFilename = new System.Windows.Forms.Label(); this.txtFilename = new System.Windows.Forms.TextBox(); this.btnBrowse = new System.Windows.Fo