用WebClient.UploadData方法上载文件数据的方法

时间:2021-05-18

假如某网站有个表单,例如(url:http://localhost/login.aspx):
帐号
密码

我们需要在程序中提交数据到这个表单,对于这种表单,我们可以使用WebClient.UploadData方法来实现,将所要上传的数据拼成字符即可,程序很简单:

stringuriString="http://localhost/login.aspx";
//创建一个新的WebClient实例.
WebClientmyWebClient=newWebClient();
stringpostData="Username=admin&Password=admin";
//注意这种拼字符串的ContentType
myWebClient.Headers.Add("Content-Type","application/x-ponent()
...{
this.lblAmigoToken=newSystem.Windows.Forms.Label();
this.txtAmigoToken=newSystem.Windows.Forms.TextBox();
this.lblFilename=newSystem.Windows.Forms.Label();
this.txtFilename=newSystem.Windows.Forms.TextBox();
this.btnBrowse=newSystem.Windows.Forms.Button();
this.txtFileData=newSystem.Windows.Forms.TextBox();
this.lblFileData=newSystem.Windows.Forms.Label();
this.btnUpload=newSystem.Windows.Forms.Button();
this.openFileDialog1=newSystem.Windows.Forms.OpenFileDialog();
this.txtResponse=newSystem.Windows.Forms.TextBox();
this.SuspendLayout();
//
//lblAmigoToken
//
this.lblAmigoToken.Location=newSystem.Drawing.Point(40,48);
this.lblAmigoToken.Name="lblAmigoToken";
this.lblAmigoToken.Size=newSystem.Drawing.Size(72,23);
this.lblAmigoToken.TabIndex=0;
this.lblAmigoToken.Text="AmigoToken";
//
//txtAmigoToken
//
this.txtAmigoToken.Location=newSystem.Drawing.Point(120,48);
this.txtAmigoToken.Name="txtAmigoToken";
this.txtAmigoToken.Size=newSystem.Drawing.Size(248,21);
this.txtAmigoToken.TabIndex=1;
this.txtAmigoToken.Text="";
//
//lblFilename
//
this.lblFilename.Location=newSystem.Drawing.Point(40,96);
this.lblFilename.Name="lblFilename";
this.lblFilename.Size=newSystem.Drawing.Size(80,23);
this.lblFilename.TabIndex=2;
this.lblFilename.Text="Filename";
//
//txtFilename
//
this.txtFilename.Location=newSystem.Drawing.Point(120,96);
this.txtFilename.Name="txtFilename";
this.txtFilename.Size=newSystem.Drawing.Size(248,21);
this.txtFilename.TabIndex=3;
this.txtFilename.Text="";
//
//btnBrowse
//
this.btnBrowse.Location=newSystem.Drawing.Point(296,144);
this.btnBrowse.Name="btnBrowse";
this.btnBrowse.TabIndex=4;
this.btnBrowse.Text="浏览...";
this.btnBrowse.Click+=newSystem.EventHandler(this.btnBrowse_Click);
//
//txtFileData
//
this.txtFileData.Location=newSystem.Drawing.Point(120,144);
this.txtFileData.Name="txtFileData";
this.txtFileData.Size=newSystem.Drawing.Size(168,21);
this.txtFileData.TabIndex=5;
this.txtFileData.Text="";
//
//lblFileData
//
this.lblFileData.Location=newSystem.Drawing.Point(40,144);
this.lblFileData.Name="lblFileData";
this.lblFileData.Size=newSystem.Drawing.Size(72,23);
this.lblFileData.TabIndex=6;
this.lblFileData.Text="FileData";
//
//btnUpload
//
this.btnUpload.Location=newSystem.Drawing.Point(48,184);
this.btnUpload.Name="btnUpload";
this.btnUpload.TabIndex=7;
this.btnUpload.Text="Upload";
this.btnUpload.Click+=newSystem.EventHandler(this.btnUpload_Click);
//
//txtResponse
//
this.txtResponse.Location=newSystem.Drawing.Point(136,184);
this.txtResponse.Multiline=true;
this.txtResponse.Name="txtResponse";
this.txtResponse.Size=newSystem.Drawing.Size(248,72);
this.txtResponse.TabIndex=8;
this.txtResponse.Text="";
//
//frmUpload
//
this.AutoScaleBaseSize=newSystem.Drawing.Size(6,14);
this.ClientSize=newSystem.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);

}
#endregion

///<summary>
///应用程序的主入口点。
///</summary>
[STAThread]
staticvoidMain()
...{
Application.Run(newfrmUpload());
}

privatevoidbtnUpload_Click(objectsender,System.EventArgse)
...{
//非空检验
if(txtAmigoToken.Text.Trim()==""||txtFilename.Text==""||txtFileData.Text.Trim()=="")
...{
MessageBox.Show("Pleasefilldata");
return;
}

//所要上传的文件路径
stringpath=txtFileData.Text.Trim();

//检查文件是否存在
if(!File.Exists(path))
...{
MessageBox.Show("{0}doesnotexist!",path);
return;
}

//读文件流
FileStreamfs=newFileStream(path,FileMode.Open,
FileAccess.Read,FileShare.Read);

//这部分需要完善
stringContentType="application/octet-stream";
byte[]fileBytes=newbyte[fs.Length];
fs.Read(fileBytes,0,Convert.ToInt32(fs.Length));


//生成需要上传的二进制数组
CreateBytescb=newCreateBytes();
//所有表单数据
ArrayListbytesArray=newArrayList();
//普通表单
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
booluploaded=cb.UploadData("http://localhost/UploadData/UploadAvatar.aspx",bytes,outresponseBytes);

//将返回的内容输出到文件
using(FileStreamfile=newFileStream(@"c:response.text",FileMode.Create,FileAccess.Write,FileShare.Read))
...{
file.Write(responseBytes,0,responseBytes.Length);
}

txtResponse.Text=System.Text.Encoding.UTF8.GetString(responseBytes);

}

privatevoidbtnBrowse_Click(objectsender,System.EventArgse)
...{
if(openFileDialog1.ShowDialog()==DialogResult.OK)
...{
txtFileData.Text=openFileDialog1.FileName;
}

}
}
}


完整的代码见附件:UploadData.rar(38K)(http://bbs.openlab.net.cn/PostAttachment.aspx?PostID=400927),解压后给web目录建个虚拟目录"UploadData",其中UploadAvatar.aspx是实际的上传处理页,如果上传成功,则返回文件名和文件类型等信息。default.aspx是asp.net页面来调用WebClient.UploadData方法提交数据,UploadDataWin项目则是winform程序调用。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章