文件上传

fileupload服务控件上传

int leng = FileUpload1.PostedFile.ContentLength;//获取文件大小

            string fileName = FileUpload1.PostedFile.FileName;//获取文件名
            if (!string.IsNullOrEmpty(fileName))
            {
                string savePath = Server.MapPath("/image/");//上传的位置
                string extName = System.IO.Path.GetExtension(fileName);//获取扩展名
                string newName = Guid.NewGuid().ToString();//新文件名
                string text = savePath + newName + extName;//路径+文件名+扩展名

                FileUpload1.PostedFile.SaveAs(text);//保存
                Response.Write("<script>alert('长传成功" + leng/1024000 + "')</script>");

            }
            else
            {
                Response.Write("<script>alert('未选择上传文件')</script>");
            }

input=“file”

 1   if (Request.HttpMethod.ToLower() == "post")//form表单添加enctype="multipart/form-data"
 2             {
 3                 HttpFileCollection files = Request.Files;//文件集合
 4                 HttpPostedFile file = files[0];//第一个文件
 5                 if (file!=null)
 6                 {
 7                     string fileName = file.FileName;//获取文件名
 8                     string ExtName = Path.GetExtension(fileName);//获取扩展名
 9                     string savePath = Server.MapPath("/image/");//保存的路径
10                     string text = savePath+ fileName + ExtName;
11                     file.SaveAs(text);//保存
12
 using (Image oldImg = Image.FromStream(file.InputStream))//缩略图
                    {
                        using (Image thumImg = new Bitmap(100, 100))
                        {
                            using (Graphics g = Graphics.FromImage(thumImg))
                            {
                                g.DrawImage(oldImg //要画的图片
                                    , new Rectangle(0, 0, 100, 100)  //表示要画到缩略图的哪个位置(画满整个缩略图)
                                    , new Rectangle(0, 0, oldImg.Width, oldImg.Height) //将原图整个画到缩略图
                                    , GraphicsUnit.Pixel); //指定单位是像素
                            }

                            //将缩略图保存到服务器的磁盘
                            //将缩略图保存到 image\
                            string thumphyPath = Server.MapPath("/image/");
                            string thumFullPath = thumphyPath + newfile; //获取缩略图的完整路径
                            thumImg.Save(thumFullPath);
                        }
                    }

 

13                 }
14             }

文件大小限制

 <system.webServer> <security> <requestFiltering> <requestLimits maxAllowedContentLength="1048576000" /><!--最大上传1G文件,--> </requestFiltering> </security> </system.webServer> 

 

3.swfupload

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="Superior.OA.UI.WebForm2" %>
 2 
 3 <!DOCTYPE html>
 4 
 5 <html xmlns="http://www.w3.org/1999/xhtml">
 6 <head runat="server">
 7     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 8     <title></title>
 9     <script src="/js/jquery-1.7.1.min.js"></script>
10     <script src="/js/SwfUpload/swfupload.js"></script>
11     <script type="text/javascript">
12         var swfu;
13         $(function () {
14             var swfOption = {
15                 upload_url: "/WebForm2.aspx",
16                 flash_url: "/js/SwfUpload/swfupload.swf",
17                 button_placeholder_id: "swfu-placeholder",
18                 file_size_limit: "20480",//大小
19                 button_width: 200, //按钮宽度
20                 button_height: 20, //按钮高度
21                 button_text: '点我选择文件',//按钮文字
22                 file_post_name: 'Filedata',//传回后台文件数组
23                 file_types_description: "All Files",//文件类型
24                 file_upload_limit: 0//文件数量限制
25 
26             }
27 
28             swfu = new SWFUpload(swfOption);
29 
30         })
31         function saveswf() {
32             swfu.startUpload();
33 
34         }
35     </script>
36 </head>
37 <body>
38     <form id="form1" runat="server">
39         <div>
40             <div id="swfu-placeholder"></div>
41             <div>
42                 <input type="button" value="上传 " onclick="saveswf()" />
43             </div>
44         </div>
45     </form>
46 </body>
47 </html>
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.UI;
 6 using System.Web.UI.WebControls;
 7 
 8 namespace Superior.OA.UI
 9 {
10     public partial class WebForm2 : System.Web.UI.Page
11     {
12         protected void Page_Load(object sender, EventArgs e)
13         {
14             int number = Context.Request.Files.Count;
15             if (number>0)
16             {
17                 getfile(number);
18             }
19          
20 
21         }
22 
23         public void getfile(int number)
24         {
25             HttpPostedFile file;
26              number = Context.Request.Files.Count;
27             for (int i = 0; i < number; i++)
28             {
29                 file = Context.Request.Files[i];
30                 string filename = file.FileName;
31                 string path = Server.MapPath("/file/");
32                 string test = path + filename;
33                 file.SaveAs(path+filename);
34             }
35         }
36     }
37 }

 

posted @ 2015-03-25 16:48  三醉  阅读(178)  评论(0编辑  收藏  举报