• c# Winform上传文件


    http://blog.csdn.net/shihuan10430049/article/details/3734398这个代码有点问题

    http://blogs.msdn.com/b/johan/archive/2006/11/15/are-you-getting-outofmemoryexceptions-when-uploading-large-files.aspx

    http://blog.csdn.net/five3/article/details/7181521

    Winform代码:

    public static void UploadFile(string strFilePath, string strSavePath, string strURL)
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
    
                string strBoundry = "------------" + System.DateTime.Now.Ticks.ToString("x");
                // The trailing boundary string
                byte[] boundaryBytes = Encoding.ASCII.GetBytes("
    --" + strBoundry + "
    ");
                StringBuilder sb = new StringBuilder();
                sb.Append("--");
                sb.Append(strBoundry);
                sb.Append("
    ");
                sb.Append("Content-Disposition: form-data; name="");
                sb.Append("A.txt");
                sb.Append(""; filename="");
                sb.Append(Path.GetFileName(strFilePath));
                sb.Append(""");
                sb.Append("
    ");
                sb.Append("Content-Type: ");
                sb.Append("application/octet-stream");
                sb.Append("
    ");
                sb.Append("
    ");
                string strPostHeader = sb.ToString();
                byte[] postHeaderBytes = Encoding.UTF8.GetBytes(strPostHeader);
    
                // The WebRequest
                HttpWebRequest oWebrequest = (HttpWebRequest)WebRequest.Create(strURL);
                oWebrequest.ContentType = "multipart/form-data; boundary=" + strBoundry;
                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(strFilePath, 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();
    
                // Clean up
                oFileStream.Close();
                oRequestStream.Close();
                s.Close();
                sr.Close();
    
                //return sReturnString;
     
            }

    aspx代码

    public partial class Save : 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("UploadDocument") + "\" + file.FileName;
                        if (!Directory.Exists(Path.GetDirectoryName(filePath)))
                        {
                            Directory.CreateDirectory(Path.GetDirectoryName(filePath));
                        }
                        file.SaveAs(filePath);
                        Response.Write("Success/r/n");
                    }
                    catch(Exception ex)
                    {
                        Response.Write("Error/r/n");
                    }
                }
            }
        }
    

      亲测可用

  • 相关阅读:
    【Docker】04 部署MySQL
    【Docker】03 基础操作
    【Nexus】Linux上的Maven私服搭建
    【Mybatis-Plus】01 快速上手
    【Java】IDEA普通JavaEE项目实现SSM整合
    【Vue】03 Slot 插槽 & 自定义事件
    【Vue】02 Component 组件 & Axios
    【Vue】01 基础语法
    【Vue】Vue-Cli 安装
    【Project】JS的Map对象前后交互问题
  • 原文地址:https://www.cnblogs.com/niuge/p/3941989.html
Copyright © 2020-2023  润新知