时间:2021-05-28
如果你需要跨域上传内容到另外一个域名并且需要获取返回值,使用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-ponent() { 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.Forms.Button(); this.txtFileData = new System.Windows.Forms.TextBox(); this.lblFileData = new System.Windows.Forms.Label(); this.btnUpload = new System.Windows.Forms.Button(); this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); this.txtResponse = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // lblAmigoToken // this.lblAmigoToken.Location = new System.Drawing.Point(40, 48); this.lblAmigoToken.Name = "lblAmigoToken"; this.lblAmigoToken.Size = new System.Drawing.Size(72, 23); this.lblAmigoToken.TabIndex = 0; this.lblAmigoToken.Text = "AmigoToken"; // // txtAmigoToken // this.txtAmigoToken.Location = new System.Drawing.Point(120, 48); this.txtAmigoToken.Name = "txtAmigoToken"; this.txtAmigoToken.Size = new System.Drawing.Size(248, 21); this.txtAmigoToken.TabIndex = 1; this.txtAmigoToken.Text = ""; // // lblFilename // this.lblFilename.Location = new System.Drawing.Point(40, 96); this.lblFilename.Name = "lblFilename"; this.lblFilename.Size = new System.Drawing.Size(80, 23); this.lblFilename.TabIndex = 2; this.lblFilename.Text = "Filename"; // // txtFilename // this.txtFilename.Location = new System.Drawing.Point(120, 96); this.txtFilename.Name = "txtFilename"; this.txtFilename.Size = new System.Drawing.Size(248, 21); this.txtFilename.TabIndex = 3; this.txtFilename.Text = ""; // // btnBrowse // this.btnBrowse.Location = new System.Drawing.Point(296, 144); this.btnBrowse.Name = "btnBrowse"; this.btnBrowse.TabIndex = 4; this.btnBrowse.Text = "浏览"; this.btnBrowse.Click += new System.EventHandler(this.btnBrowse_Click); // // txtFileData // this.txtFileData.Location = new System.Drawing.Point(120, 144); this.txtFileData.Name = "txtFileData"; this.txtFileData.Size = new System.Drawing.Size(168, 21); this.txtFileData.TabIndex = 5; this.txtFileData.Text = ""; // // lblFileData // this.lblFileData.Location = new System.Drawing.Point(40, 144); this.lblFileData.Name = "lblFileData"; this.lblFileData.Size = new System.Drawing.Size(72, 23); this.lblFileData.TabIndex = 6; this.lblFileData.Text = "FileData"; // // btnUpload // this.btnUpload.Location = new System.Drawing.Point(48, 184); this.btnUpload.Name = "btnUpload"; this.btnUpload.TabIndex = 7; this.btnUpload.Text = "Upload"; this.btnUpload.Click += new System.EventHandler(this.btnUpload_Click); // // txtResponse // this.txtResponse.Location = new System.Drawing.Point(136, 184); this.txtResponse.Multiline = true; this.txtResponse.Name = "txtResponse"; this.txtResponse.Size = new System.Drawing.Size(248, 72); this.txtResponse.TabIndex = 8; this.txtResponse.Text = ""; // // frmUpload // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(400, 269); this.Controls.Add(this.txtResponse); this.Controls.Add(this.btnUpload); this.Controls.Add(this.lblFileData); this.Controls.Add(this.txtFileData); this.Controls.Add(this.btnBrowse); this.Controls.Add(this.txtFilename); this.Controls.Add(this.lblFilename); this.Controls.Add(this.txtAmigoToken); this.Controls.Add(this.lblAmigoToken); this.Name = "frmUpload"; this.Text = "frmUpload"; this.ResumeLayout(false); } [STAThread] static void Main() { Application.Run(new frmUpload()); } private void btnUpload_Click(object sender, System.EventArgs e) { // 非空检验 if (txtAmigoToken.Text.Trim() == "" || txtFilename.Text == "" || txtFileData.Text.Trim() == "") { MessageBox.Show("Please fill data"); return; } // 所要上传的文件路径 string path = txtFileData.Text.Trim(); // 检查文件是否存在 if (!File.Exists(path)) { MessageBox.Show("{0} does not exist!", path); return; } // 读文件流 FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read); // 这部分需要完善 string ContentType = "application/octet-stream"; byte[] fileBytes = new byte[fs.Length]; fs.Read(fileBytes, 0, Convert.ToInt32(fs.Length)); // 生成需要上传的二进制数组 CreateBytes cb = new CreateBytes(); // 所有表单数据 ArrayList bytesArray = new ArrayList(); // 普通表单 bytesArray.Add(cb.CreateFieldData("FileName", txtFilename.Text)); bytesArray.Add(cb.CreateFieldData("AmigoToken", txtAmigoToken.Text)); // 文件表单 bytesArray.Add(cb.CreateFieldData("FileData", path , ContentType, fileBytes)); // 合成所有表单并生成二进制数组 byte[] bytes = cb.JoinBytes(bytesArray); // 返回的内容 byte[] responseBytes; // 上传到指定Url bool uploaded = cb.UploadData("http://localhost/UploadData/UploadAvatar.aspx", bytes, out responseBytes); // 将返回的内容输出到文件 using (FileStream file = new FileStream(@"c: esponse.text", FileMode.Create, FileAccess.Write, FileShare.Read)) { file.Write(responseBytes, 0, responseBytes.Length); } txtResponse.Text = System.Text.Encoding.UTF8.GetString(responseBytes); } private void btnBrowse_Click(object sender, System.EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { txtFileData.Text = openFileDialog1.FileName; } } }}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
小编之前也介绍了许多ASP.NET文件上传的解决案例,今天来个asp.net文件上传大集合。1使用标准HTML来进行图片上传前台代码:使用标准HTML来进行图片
基于ASP.Net+easyUI框架上传图片,实现图片上传,提交表单://检查图片的格式是否正确,同时实现预览functionsetImagePreview(o
一、目前在ASP.NET中页面传值共有这么几种方式:1、表单提交,....form1.submit();....此种方在ASP。NET中无效,因为ASP。NET
本文实例讲述了Asp.Net的FileUpload类实现上传文件的方法。分享给大家供大家参考。具体功能代码如下:复制代码代码如下:usingSystem;usi
本文实例讲述了asp.net中MVC借助Iframe实现无刷新上传文件的方法。分享给大家供大家参考。具体实现方法如下:html:复制代码代码如下:选择文件:Ca