在做WinForm时,需要WinForm同Web进行交互,有时需要进行图片、文件的上传操作.
WinForm的代码如下:
private void button2_Click(object sender, EventArgs e)
{
// string a = MyUploader(this.textBox1.Text.Trim(), @"http://localhost:4451/TEST/Default.aspx");
string a = MyUploader(this.textBox1.Text.Trim(), @"http://localhost/release/Default.aspx");
MessageBox.Show(a);
}
public static string MyUploader(string strFileToUpload, string strUrl)
{
string strFileFormName = "file";
Uri oUri = new Uri(strUrl);
string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x");
// The trailing boundary string
byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + strBoundary + "\r\n");
// The post message header
StringBuilder sb = new StringBuilder();
sb.Append("--");
sb.Append(strBoundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"");
sb.Append(strFileFormName);
sb.Append("\"; filename=\"");
sb.Append(Path.GetFileName(strFileToUpload));
sb.Append("\"");
sb.Append("\r\n");
sb.Append("Content-Type: ");
sb.Append("application/octet-stream");
sb.Append("\r\n");
sb.Append("\r\n");
string strPostHeader = sb.ToString();
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(strPostHeader);
// The WebRequest
HttpWebRequest oWebrequest = (HttpWebRequest)WebRequest.Create(oUri);
oWebrequest.ContentType = "multipart/form-data; boundary=" + strBoundary;
oWebrequest.Method = "POST";
// This is important, otherwise the whole file will be read to memory anyway...
oWebrequest.AllowWriteStreamBuffering = false;
// Get a FileStream and set the final properties of the WebRequest
FileStream oFileStream = new FileStream(strFileToUpload, FileMode.Open, FileAccess.Read);
long length = postHeaderBytes.Length + oFileStream.Length + boundaryBytes.Length;
oWebrequest.ContentLength = length;
Stream oRequestStream = oWebrequest.GetRequestStream();
// Write the post header
oRequestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
// Stream the file contents in small pieces (4096 bytes, max).
byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)oFileStream.Length))];
int bytesRead = 0;
while ((bytesRead = oFileStream.Read(buffer, 0, buffer.Length)) != 0)
oRequestStream.Write(buffer, 0, bytesRead);
oFileStream.Close();
// Add the trailing boundary
oRequestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
WebResponse oWResponse = oWebrequest.GetResponse();
Stream s = oWResponse.GetResponseStream();
StreamReader sr = new StreamReader(s);
String sReturnString = sr.ReadToEnd();
Console.WriteLine(sReturnString);
// Clean up
oFileStream.Close();
oRequestStream.Close();
s.Close();
sr.Close();
return sReturnString;
}
在按钮点击时,便会触发提交操作,Web的Default.aspx代码如下:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Files.Count > 0)
{
try
{
HttpPostedFile file = Request.Files[0];
string filePath = this.MapPath("FileUpload") + "\\" + file.FileName;
file.SaveAs(filePath);
Response.Write("成功了");
}
catch (Exception)
{
Response.Write("失败了1");
}
}
else
{
Response.Write("失败了2");
}
Response.End();
}
}
这样,便可以将文件通过winform 上传到web 服务器上。
如果服务器端是Java的Web,则可以借助SmartUpload组件,Java端上传的Servlet如下:
package com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
public class UpLoad extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
SmartUpload mySmartUpload = new SmartUpload();
mySmartUpload.initialize(getServletConfig(), request, response);
mySmartUpload.upload();
Files files = mySmartUpload.getFiles();
int num = files.getCount();
for (int i = 0; i < num; i++) {
com.jspsmart.upload.File myfile = files.getFile(i);
String fileName = myfile.getFileName();
fileName = new String(fileName.getBytes(), "UTF-8");//强转成UTF8不然中文文件名会是乱码
myfile.saveAs("/upload/" + fileName, i + 1);
}
String ret = "OK";
byte[] rep = ret.getBytes();
response.getOutputStream().write(rep);
} catch (SmartUploadException e) {
e.printStackTrace();
}
}
}
至此,WinForm上传文件到Web项目,服务器端为.Net和Java的,均可以使用。
分享到:
相关推荐
c#、ASP.Net、Winform、WebApplication、WebService 编程150例源码 ,《C#编程150例》的附带源码文件,可修改可编译
cad lib最新版) 未破解 希望有高手能破解cadlib dll 可以在线生成cad档(在线读取CAD档未写demo 如果需要可以与我联系mansai@163 com 如果有高手可以破解dll档请与我联系) 网上很多cadlib的破解只能用在winform里 ...
技术基础 New Folder 多样式星期名字转换 [Design, C#] .NET关于string转换的一个...C#+ASP.NET开发基于Web的RSS阅读器 ASP.NET RSS Toolkit(RSS工具) Serialize Your Deck with Positron [XML Serialization, XSD, C#]...
ASP.Net MVC是微软推出的区别于ASP.Net WebForm的Web开发新技术,由于ASP.Net MVC解决了ASP.Net WebForm的很多缺点,非常适合大型、中型项目的开发,一经推出就受到了.Net开发社区的追捧,很多.Net开发人员的职位...
第1章 C# Windows编程基础 第2章 图形图象处理 第3章 多媒体应用 第4章 系统文件处理 第5章 数据库应用 第6章 C#网络开发 第7章 C# WEB编程应用 第8章 C# 综合实例编程应用
深入理解C#编程中的组件-事件-委托.txt 生成透明的GIF图片 .txt 生成压缩图片时的2种方法.txt 使用C#返回系统特殊路径.txt 使用VisualC#.NET检查Windows版本.txt 使用Win32和其他库.txt 使用自己画的光标.txt...
二 .NET框架&C#编程 面向对象的基本知识体系 .NET中的面向对象设计与应用 C#与设计模式 泛型 C#中的新特性 程序集 .NET应用程序部署和版本控制 委托和事件 线程 反射 对象序列化 数据流和文件 Internet...
第三篇主要关注网络方面的知识,介绍了C#点对点通讯编程、Internet访问,重点讲述了Web开发的相关模型,包括ASP.NET Web开发、ASP.NET Ajax、Web服务等,并引入最新的面向服务编程模型WCF和面向描述编程模型WPF,...
1.6.1 如何创建ASP.NET项目 58 1.6.2 如何创建Windows项目 61 1.6.3 Visual Studio.NET 2008 操作与使用技巧 61 1.6.4 常见开发调试技巧 66 1.6.5 错误异常处理方法 70 本章常见技术面试题 76 常见面试技巧之面试...
深入理解C#编程中的组件-事件-委托.txt 生成透明的GIF图片 .txt 生成压缩图片时的2种方法.txt 使用C#返回系统特殊路径.txt 使用VisualC#.NET检查Windows版本.txt 使用Win32和其他库.txt 使用自己画的光标.txt...
1.6.1 如何创建ASP.NET项目 58 1.6.2 如何创建Windows项目 61 1.6.3 Visual Studio.NET 2008 操作与使用技巧 61 1.6.4 常见开发调试技巧 66 1.6.5 错误异常处理方法 70 本章常见技术面试题 76 常见面试技巧之面试...
C#基础(oop,IO,泛型,反射,多线程和异步,委托和事件,序列化,事务,windows服务,注册表,com,...asp.net asp.net mvc winform(自带控件,自定义控件,dev控件,dotnetbar等) web service,wcf,remoting等
51编程-C#敏捷开发框架源码特点 ...5.可扩展支持Remoting、Web Services、Asp.net等不同结构系统 6.所有数据层、UI层代码由“51编程.代码器”自动生成 7.源码中包含“代码器”插件源码及模板源码,可自行修改
2.9 C#编程规则 46 2.10 内容总结 50 2.11 独立实践 50 3 对象和结构 52 3.1 类和结构 52 3.2 类和成员 53 3.3 构造函数 60 3.4 结构 65 3.5 部分类 68 3.6 内容总结 72 3.7 独立实践 72 4 继承 73 4.1 继承的类型 ...
c#编程集合资料,不错的应用设计,另附一些开发案例的下载地址,欢迎大家下载学习。 学习完本课程之后,我们需要达到以下目标: (1) 掌握基本的C#语法,熟练运用C#编写基本的程序逻辑。 (2) 能够创建C#控制台...
这是<C#范例宝典>的配套光盘,里面含有500多个实例,里面包含了如:winForm、数据库应用、web编程和图形图像设计和网络编程等实例。是用visual C#2005.NET做的,很实用,希望对大家有所帮助,此程序有6个分压缩包:...
本书专门讲述了GDI+编程中与开发自定义控件相关的功能,重点放在Windows Forms自定义控件的创建上,同时也讨论了ASP.NET服务器控件的创建问题。全书共分为4个部分:第1部分是对GDI+的用途和结构的概述,第2部 分...
这是<C#范例宝典>的配套光盘,里面含有500多个实例,里面包含了如:winForm、数据库应用、web编程和图形图像设计和网络编程等实例。是用visual C#2005.NET做的,很实用,希望对大家有所帮助