winfrom里面的代码

   private void button1_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(textBox1.Text.Trim()))
{
string a = MyUploader(this.textBox1.Text.Trim(), @"http://localhost:63715/Default.aspx"); //这里的路径就是网页路径
MessageBox.Show(a);
}
else
{
MessageBox.Show("请选择需要上传的文件");
} } /// <summary>
/// 文件上传
/// </summary>
/// <param name="strFileToUpload">文件名</param>
/// <param name="strUrl">目标路径</param>
/// <returns>是否成功</returns>
public static string MyUploader(string strFileToUpload, string strUrl)
{
string strFileFormName = "file";
Uri oUri = new Uri(strUrl); //创建一个url请求
//分隔符
string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x");
//转成ascii码
byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + strBoundary + "\r\n");
//使用multipart/form-data请求体格式为(包含文件内容)
//Content-Disposition: form-data; name="fileparam"; filename="test.txt"
//Content-Type: text/plain 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);
//post提交
HttpWebRequest oWebrequest = (HttpWebRequest)WebRequest.Create(oUri);//指定url
oWebrequest.ContentType = "multipart/form-data; boundary=" + strBoundary; //固定格式
oWebrequest.Method = "POST";
oWebrequest.AllowWriteStreamBuffering = false; FileStream oFileStream = new FileStream(strFileToUpload, FileMode.Open, FileAccess.Read);
long length = postHeaderBytes.Length + oFileStream.Length + boundaryBytes.Length;
oWebrequest.ContentLength = length;//确定传的长度
Stream oRequestStream = oWebrequest.GetRequestStream();//转成流
oRequestStream.Write(postHeaderBytes, , postHeaderBytes.Length); //checked((uint)Math.Min)
//文件流的长度
byte[] buffer = new Byte[checked((uint)Math.Min(, (int)oFileStream.Length))];
int bytesRead = ;
while ((bytesRead = oFileStream.Read(buffer, , buffer.Length)) != )
oRequestStream.Write(buffer, , bytesRead);
oFileStream.Close(); oRequestStream.Write(boundaryBytes, , boundaryBytes.Length);
WebResponse oWResponse = oWebrequest.GetResponse();
Stream s = oWResponse.GetResponseStream(); //从webfrom中返回的数据
StreamReader sr = new StreamReader(s);
String sReturnString = sr.ReadToEnd();
Console.WriteLine(sReturnString);
//用完关闭
oFileStream.Close();
oRequestStream.Close();
s.Close();
sr.Close();
return sReturnString;
}

webFrom里面的代码

 protected void Page_Load(object sender, EventArgs e)
{
if (Request.Files.Count > )
{
try
{
HttpPostedFile file = Request.Files[]; //获取传的文件
string filePath = this.MapPath("FileUpload") + "\\" + file.FileName;
file.SaveAs(filePath);
Response.Write("成功了");
}
catch (Exception)
{
Response.Write("失败了1");
}
}
else
{
Response.Write("失败了2");
} Response.End();
}

winfrom中上传文件保存在webFrom里面的更多相关文章

  1. vue中上传文件之multipart/form-data

    首先在项目里用了拦截器的,由于拦截器会将传递的参数转成对象,所以你i提交的时候会发现multipart/form-data或转变成application/json 其次关于input的文件上传是需要一 ...

  2. JavaScript中上传文件为图片如何读取+JS中如何使用clip()剪切指定区域(UI组件之图片剪裁器)

    File读取和FileReader() //获取上传的文件/图片 function getFile(){ var files,len; var reader = new FileReader(); v ...

  3. 【解决】AWS服务控制台中上传文件失败

    使用IE 11,在 AWS Services Console 中不管是 S3 还是 Elastic Beanstalk 的页面中上传页面都会失败,提示信息如下: A problem occurred ...

  4. 谈谈php中上传文件的处理

    这是一个表单的时代... 我们在浏览器中编辑自己的信息,会遇到上传头像:在文库中,我们会上传文档......到处存在“上传”这个词. php是最好的语言(其他语言的程序猿们不要打我...).php在处 ...

  5. ASP.Net中上传文件的几种方法

    在做Web项目时,上传文件是经常会碰到的需求.ASP.Net的WebForm开发模式中,封装了FileUpload控件,可以方便的进行文件上传操作.但有时,你可能不希望使用ASP.Net中的服务器控件 ...

  6. 现在,以编程方式在 Electron 中上传文件,是非常简单的!

    必要的上下文 想尽快熟悉上下文语境的,可以点这里: https://github.com/electron/electron/issues/749 这段讨论,其实本来是讨论如何自动设置 input 标 ...

  7. element-ui中上传文件upload

    <el-upload class="upload-demo" name="targetFile" ref="upload" :with ...

  8. Mac/Linux/Centos终端中上传文件到Linux云服务器

      1.mac上传文件到Linux服务器  scp 文件名 用户名@服务器ip:目标路径 如:scp /Users/test/testFile test@www.linuxidc.com:/test/ ...

  9. vue中上传文件相同文件名没反应

    vue项目中会遇到上传文件的需求,jquery会有一些插件很方便,如果不使用插件网上的方法没有太容易的而且很多是原生JS或者基于jQuery操作dom结构的.那么在vue项目中如何实现呢,还有如何模拟 ...

随机推荐

  1. C# 序列化(Binary、Xml、Soap)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  2. RestTemplate中几种常见的请求方式

    GET请求 第一种:getForEntity getForEntity方法的返回值是一个ResponseEntity<T>,ResponseEntity<T>是Spring对H ...

  3. 浅谈jrebel

    有个同事提高个jrebel的工具,提起tomcat的热部署方案. jrebel是一款收费的JVM级的热部署工具包. JVM级的热部署也就是说,可以不重启JVM,让修改或添加的类加载到JVM中. 加载器 ...

  4. 使用WindowsAPI播放PCM音频

    这一篇文章同上一篇<使用WindowsAPI获取录音音频>原理具有相似之处,不再详细介绍函数与结构体的参数 1. waveOutGetNumDevs 2. waveOutGetDevCap ...

  5. 设计模式在Spring

    设计模式在spring中的使用1.工厂模式,这个很明显,在各种BeanFactory以及ApplicationContext创建中都用到了:2.模版模式,这个也很明显,在各种BeanFactory以及 ...

  6. 详解HBase架构原理

    一.什么是HBase           HBase是一个高可靠.高性能.面向列.可伸缩的分布式存储系统,利用HBase技术可在廉价的PC Server上搭建大规模结构化存储集群.         H ...

  7. Excel数据导入数据库

    maven依赖 <!--excel相关依赖--> <dependency> <groupId>org.apache.poi</groupId> < ...

  8. hdu 3586 最小代价切断根与叶子联系

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3586 http://blog.csdn.net/woshi250hua/article/details ...

  9. python面试题——爬虫相关

    1.接触过几种爬虫模块 urllib.requests这两种爬虫模块. 2.robots协议是什么? 它就是一个防君子不防小人的协议,网站有一些数据不想被爬虫程序爬取,可以编写robots协议文件,明 ...

  10. foxmail6.5 不能收取电子邮件,反复提示输入密码?

    使用foxmail时候报错:-err system resource error,system close connect,code=<1014>,id=<1>重新输入密码吗? ...