国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問(wèn)答1問(wèn)答10問(wèn)答100問(wèn)答1000問(wèn)答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問(wèn)答文章1 問(wèn)答文章501 問(wèn)答文章1001 問(wèn)答文章1501 問(wèn)答文章2001 問(wèn)答文章2501 問(wèn)答文章3001 問(wèn)答文章3501 問(wèn)答文章4001 問(wèn)答文章4501 問(wèn)答文章5001 問(wèn)答文章5501 問(wèn)答文章6001 問(wèn)答文章6501 問(wèn)答文章7001 問(wèn)答文章7501 問(wèn)答文章8001 問(wèn)答文章8501 問(wèn)答文章9001 問(wèn)答文章9501
當(dāng)前位置: 首頁(yè) - 科技 - 知識(shí)百科 - 正文

Asp.Net模擬表單提交數(shù)據(jù)和上傳文件的實(shí)現(xiàn)代碼

來(lái)源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 22:38:57
文檔

Asp.Net模擬表單提交數(shù)據(jù)和上傳文件的實(shí)現(xiàn)代碼

Asp.Net模擬表單提交數(shù)據(jù)和上傳文件的實(shí)現(xiàn)代碼:如果你需要跨域上傳內(nèi)容到另外一個(gè)域名并且需要獲取返回值,使用Asp.Net的作為代理是最好的辦法,要是客戶端直接提交到iframe中,由于跨域是無(wú)法用javascript獲取到iframe中返回的內(nèi)容的。此時(shí)需要在自己的網(wǎng)站做一個(gè)動(dòng)態(tài)頁(yè)作為代理,將表單提交到動(dòng)態(tài)頁(yè),動(dòng)
推薦度:
導(dǎo)讀Asp.Net模擬表單提交數(shù)據(jù)和上傳文件的實(shí)現(xiàn)代碼:如果你需要跨域上傳內(nèi)容到另外一個(gè)域名并且需要獲取返回值,使用Asp.Net的作為代理是最好的辦法,要是客戶端直接提交到iframe中,由于跨域是無(wú)法用javascript獲取到iframe中返回的內(nèi)容的。此時(shí)需要在自己的網(wǎng)站做一個(gè)動(dòng)態(tài)頁(yè)作為代理,將表單提交到動(dòng)態(tài)頁(yè),動(dòng)

如果你需要跨域上傳內(nèi)容到另外一個(gè)域名并且需要獲取返回值,使用Asp.Net的作為代理是最好的辦法,要是客戶端直接提交到iframe中,由于跨域是無(wú)法用javascript獲取到iframe中返回的內(nèi)容的。此時(shí)需要在自己的網(wǎng)站做一個(gè)動(dòng)態(tài)頁(yè)作為代理,將表單提交到動(dòng)態(tài)頁(yè),動(dòng)態(tài)頁(yè)負(fù)責(zé)將表單的內(nèi)容使用WebClient或HttpWebRequest將表單數(shù)據(jù)再上傳到遠(yuǎn)程服務(wù)器,由于在服務(wù)器端進(jìn)行操作,就不存在跨域問(wèn)題了。

WebClient上傳只包含鍵值對(duì)的文本信息示例代碼:

代碼如下:
string uriString = "http://localhost/login.aspx";
// 創(chuàng)建一個(gè)新的 WebClient 實(shí)例.
WebClient myWebClient = new WebClient();
string postData = "Username=admin&Password=admin";
// 注意這種拼字符串的ContentType
myWebClient.Headers.Add("Content-Type","application/x-www-form-urlencoded");
// 轉(zhuǎn)化成二進(jìn)制數(shù)組
byte[] byteArray = Encoding.ASCII.GetBytes(postData);
// 上傳數(shù)據(jù),并獲取返回的二進(jìn)制數(shù)據(jù).
byte[] responseArray = myWebClient.UploadData(uriString,"POST",byteArray);

WebClient上傳只包含文件的示例代碼:

代碼如下:
String uriString = "http://localhost/uploadFile.aspx";
// 創(chuàng)建一個(gè)新的 WebClient 實(shí)例.
WebClient myWebClient = new WebClient();
string fileName = @"C:/upload.txt";
// 直接上傳,并獲取返回的二進(jìn)制數(shù)據(jù).
byte[] responseArray = myWebClient.UploadFile(uriString,"POST",fileName);

 對(duì)于既包含文件又包含文本鍵值對(duì)信息的示例代碼,需要構(gòu)造表單提交的內(nèi)容,對(duì)于學(xué)asp的同學(xué)來(lái)說(shuō),下面的表單提交內(nèi)容一定不會(huì)陌生
代碼如下:
-----------------------------7d429871607fe
Content-Disposition: form-data; name="file1"; filename="G:/homepage.txt"
Content-Type: text/plain
腳本之家://www.gxlcms.com
-----------------------------7d429871607fe
Content-Disposition: form-data; name="filename"
default filename
-----------------------------7d429871607fe--

  所以只要拼一個(gè)這樣的byte[] data數(shù)據(jù)Post過(guò)去,就可以達(dá)到同樣的效果了。但是一定要注意,對(duì)于這種帶有文件上傳的,其ContentType是不一樣的,例如上面的這種,其ContentType為"multipart/form-data; boundary=---------------------------7d429871607fe"。有了ContentType,我們就可以知道boundary(就是上面的"---------------------------7d429871607fe"),知道boundary了我們就可以構(gòu)造出我們所需要的byte[] data了,最后,不要忘記,把我們構(gòu)造的ContentType傳到WebClient中(例如:webClient.Headers.Add("Content-Type", ContentType);)這樣,就可以通過(guò)WebClient.UploadData 方法上載文件數(shù)據(jù)了。

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;
 // 加上結(jié)束邊界
 string endBoundary = Boundary + "-- ";
 byte[] endBoundaryBytes = encoding.GetBytes(endBoundary);
 byteArrays.Add(endBoundaryBytes);
 foreach (byte[] b in byteArrays)
 {
 length += b.Length;
 }
 byte[] bytes = new byte[length];
 // 遍歷復(fù)制
 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;
 }
 /// 獲取普通表單區(qū)域二進(jìn)制數(shù)組
 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} ";
 // 頭數(shù)據(jù)
 string data = String.Format(textTemplate, fieldName, filename, contentType);
 byte[] bytes = encoding.GetBytes(data);
 // 尾數(shù)據(jù)
 byte[] endBytes = encoding.GetBytes(end);
 // 合成后的數(shù)組
 byte[] fieldData = new byte[bytes.Length + fileBytes.Length + endBytes.Length];
 bytes.CopyTo(fieldData, 0); // 頭數(shù)據(jù)
 fileBytes.CopyTo(fieldData, bytes.Length); // 文件的二進(jìn)制數(shù)據(jù)
 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.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)
 {
 // 非空檢驗(yàn)
 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));
 // 生成需要上傳的二進(jìn)制數(shù)組
 CreateBytes cb = new CreateBytes();
 // 所有表單數(shù)據(jù)
 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));
 // 合成所有表單并生成二進(jìn)制數(shù)組
 byte[] bytes = cb.JoinBytes(bytesArray);
 // 返回的內(nèi)容
 byte[] responseBytes;
 // 上傳到指定Url
 bool uploaded = cb.UploadData("http://localhost/UploadData/UploadAvatar.aspx", bytes, out responseBytes);
 // 將返回的內(nèi)容
輸出到文件 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; } } } }

聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

Asp.Net模擬表單提交數(shù)據(jù)和上傳文件的實(shí)現(xiàn)代碼

Asp.Net模擬表單提交數(shù)據(jù)和上傳文件的實(shí)現(xiàn)代碼:如果你需要跨域上傳內(nèi)容到另外一個(gè)域名并且需要獲取返回值,使用Asp.Net的作為代理是最好的辦法,要是客戶端直接提交到iframe中,由于跨域是無(wú)法用javascript獲取到iframe中返回的內(nèi)容的。此時(shí)需要在自己的網(wǎng)站做一個(gè)動(dòng)態(tài)頁(yè)作為代理,將表單提交到動(dòng)態(tài)頁(yè),動(dòng)
推薦度:
  • 熱門焦點(diǎn)

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 国产在线观看免费一级 | 欧美在线1 | 国模吧双双大尺度炮交gogo | 欧美色图一区 | 日韩一区二区三区在线 | 欧美中出在线 | 在线观看国产亚洲 | 国产一区二区三区成人久久片 | 精品国产一区二区三区香蕉 | 一区二区三区视频在线 | 91网红福利精品区一区二 | 国产高清在线 | 日本一区二区三区精品视频 | 国产特级全黄一级毛片不卡 | 国产精品毛片久久久久久久 | 国产日本在线 | 亚洲天堂欧美 | 欧美精品在线视频 | 一级毛片一级毛片一级级毛片 | 日韩高清在线观看 | 全免费午夜一级毛片一级毛 | 国产美女视频黄a视频全免费网站 | 亚洲专区欧美 | 久久精品一级 | 黄色在线免费观看网址 | 看全色黄大色大片免费久久 | 美国美女一级毛片免费全 | 91中文字幕在线播放 | 欧美天天干 | 国产又黄又爽的视频 | 日韩免费一区二区三区在线 | 亚洲欧洲视频在线 | 日韩影片在线观看 | 在线亚洲欧美日韩 | 欧美日本中文字幕 | 国产91精品久久久久久久 | 亚洲欧美在线免费观看 | 欧美成人精品第一区二区三区 | 欧美 日韩 国产 色 欧美 日韩 中文 | 精品国内自产拍在线视频 | 国产欧美曰韩一区二区三区 |