C# winform 上传文件 (多种方案)

 

转:http://www.cnblogs.com/7in10/archive/2008/05/20/1203402.html

方案一:

注意:要开启虚拟目录的“写入”权限,要不然就报 403 错误

工作中用到winform上传文件(-_-!,很少用winform,搞了半天)
碰到一点问题,解决如下
1、501 为实现错误
解决方法:
先把IISWEB服务扩展中的WebDev打开
然后
IIS站点添加MIME  txt类型 常见的MIME类型如下
超文本标记语言文本 .html,.html text/html
普通文本 .txt text/plain
RTF文本 .rtf application/rtf
GIF图形 .gif image/gif
JPEG图形 .ipeg,.jpg image/jpeg
au声音文件 .au audio/basic
MIDI音乐文件 mid,.midi audio/midi,audio/x-midi
RealAudio音乐文件 .ra, .ram audio/x-pn-realaudio
MPEG文件 .mpg,.mpeg video/mpeg
AVI文件 .avi video/x-msvideo
GZIP文件 .gz application/x-gzip
TAR文件 .tar application/x-tar
再然后
设置目标文件夹的可写性

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Net;
using  System.IO;

namespace  Common
{
    
///   <summary>
    
///  winform形式的文件传输类
    
///   </summary>

     public   class  WinFileTransporter
    
{
        
///   <summary>
        
///  WebClient上传文件至服务器,默认不自动改名
        
///   </summary>
        
///   <param name="fileNamePath"> 文件名,全路径格式 </param>
        
///   <param name="uriString"> 服务器文件夹路径 </param>

         public   void  UpLoadFile( string  fileNamePath,  string  uriString)
        
{
            UpLoadFile(fileNamePath, uriString, 
false );
        }

        
///   <summary>
        
///  WebClient上传文件至服务器
        
///   </summary>
        
///   <param name="fileNamePath"> 文件名,全路径格式 </param>
        
///   <param name="uriString"> 服务器文件夹路径 </param>
        
///   <param name="IsAutoRename"> 是否自动按照时间重命名 </param>

         public   void  UpLoadFile( string  fileNamePath,  string  uriString,  bool  IsAutoRename)
        
{
            
string  fileName  =  fileNamePath.Substring(fileNamePath.LastIndexOf( " // " +   1 );
            
string  NewFileName  =  fileName;
            
if  (IsAutoRename)
            
{
                NewFileName 
=  DateTime.Now.ToString( " yyMMddhhmmss " +  DateTime.Now.Millisecond.ToString()  +  fileNamePath.Substring(fileNamePath.LastIndexOf( " . " ));
            }


            
string  fileNameExt  =  fileName.Substring(fileName.LastIndexOf( " . " +   1 );
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是C# WinForm文件的示例代码: ```csharp private void btnUpload_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); if (openFileDialog.ShowDialog() == DialogResult.OK) { string filePath = openFileDialog.FileName; string url = "http://localhost/UploadFileWeb/WebForm1.aspx"; string paramName = "file"; HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.Method = "POST"; string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x"); byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); request.ContentType = "multipart/form-data; boundary=" + boundary; using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(boundaryBytes, 0, boundaryBytes.Length); string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n"; string header = string.Format(headerTemplate, paramName, Path.GetFileName(filePath)); byte[] headerBytes = Encoding.UTF8.GetBytes(header); requestStream.Write(headerBytes, 0, headerBytes.Length); using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { byte[] buffer = new byte[4096]; int bytesRead = 0; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) { requestStream.Write(buffer, 0, bytesRead); } } byte[] trailerBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n"); requestStream.Write(trailerBytes, 0, trailerBytes.Length); } using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { using (StreamReader reader = new StreamReader(response.GetResponseStream())) { string responseText = reader.ReadToEnd(); MessageBox.Show(responseText); } } } } ``` 该示例代码使用HttpWebRequest类向指定的URL上文件。在上文件之前,需要设置请求的Method为POST,ContentType为multipart/form-data,并设置请求头部的边界(boundary)。然后,将文件内容写入请求流中,最后发送请求并获取响应。在获取响应后,可以从响应流中读取服务器返回的内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值