zoukankan      html  css  js  c++  java
  • 多文件上传实现

    利用Request.Files对象
    代码:
    public static bool SaveResourceFile(System.Web.UI.Page page, System.Web.HttpFileCollection hfc,string resourcePath)
    {
      
    for (int i = 0; (i < hfc.Count); i++)
      
    {
         HttpPostedFile file 
    = hfc.Get(i);
         
    if (file.ContentLength > 0)
         
    {
            
    try
                        
    {
                            
    if (!Directory.Exists(page.Server.MapPath(resourcePath)))
                            
    {
                                DirectoryInfo di 
    = Directory.CreateDirectory(page.Server.MapPath(resourcePath));
                            }

                            
    if (file.FileName.LastIndexOf("\\"!= -1)
                            
    {
                                fileName 
    = file.FileName.Substring(file.FileName.LastIndexOf("\\")+1);
                            }


                            
    //将输入流转换成Byte[]
                            byte[] buffer = new byte[file.ContentLength];
                            file.InputStream.Read(buffer, 
    0, buffer.Length);

                            
    //存上传的文件
                            WriteFile(page.Server.MapPath(resourcePath + fileName), buffer);
                        }

                        
    catch (Exception err)
                        
    {
                        }

         }

       }


    }

    WriteFile方法的代码:
            public static void WriteFile(string fileName,byte[] buf)
            
    {
                FileStream f 
    = File.OpenWrite(fileName);
                f.Write(buf,
    0,buf.Length);
                f.Close();
            }

    实现客户端多文本上传服务器容易产生错误的地方是客户端文件路径和服务器路径混淆不清,其他其实和简单
  • 相关阅读:
    Python3三位运算
    PyThon3函数的使用
    PyThon3类的基本使用
    PyThon3类的继承
    Python3方法重写
    【BZOJ3307】雨天的尾巴-线段树合并+树上差分
    【CF893F】Subtree Minimum Query-主席树
    【BZOJ2212】Tree Rotations(POI2011)-平衡树启发式合并
    【BZOJ2733】永无乡(HNOI2012)-平衡树启发式合并
    【BZOJ3160】万径人踪灭-FFT+Manacher
  • 原文地址:https://www.cnblogs.com/qingwa008/p/1173381.html
Copyright © 2011-2022 走看看