服务器保存上载文件

下面的代码示例演示如何将客户端上载的所有文件保存到 Web 服务器的本地磁盘上的 C:TempFiles 文件夹中。

String TempFileName;
 HttpFileCollection MyFileCollection = Request.Files;

 for (int Loop1 = 0; Loop1 < MyFileCollection.Count; Loop1++)
 {
  TempFileName = "C:\TempFiles\File_" + Loop1.ToString(); MyFileCollection[Loop1].SaveAs(TempFileName); }



下面的代码示例演示将文件流保存到相对路径Template文件夹下

foreach (string f in Request.Files.AllKeys)
            {
                HttpPostedFileBase file = Request.Files[f];
                if (file != null && file.ContentLength > 0)
                {
                    string path = Server.MapPath("~/Template/");//获取uplaod文件夹路径
                    try
                    {
                        file.SaveAs(path + file.FileName);//保存文件
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                }
                else
                {
                    //文件为空的处理
                }
            }
原文地址:https://www.cnblogs.com/lanke0/p/5147336.html