/**//// /// 应用程序的主入口点。 /// [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:\response.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; } } } } 完整的代码见附件: UploadData.rar(38K)(http://bbs.openlab.net.cn/PostAttachment.aspx?PostID=400927),解压后给web目录建个虚拟目录"UploadData",其中UploadAvatar.aspx是实际的上传处理页,如果上传成功,则返回文件名和文件类型等信息。default.aspx是asp.net页面来调用 WebClient.UploadData方法提交数据,UploadDataWin项目则是winform程序调用。
页次:5/5 1篇/页 首页 上一页 下一页 尾页 合计 5 篇 Go: |