码迷,mamicode.com
首页 > Web开发 > 详细

使用input=file上传

时间:2015-07-16 11:11:40      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

前台,form的target指向iframe

            <form action="/EmailHandler.ashx?action=upload" id="form1" name="form1" enctype="multipart/form-data" method="post" target="hidden_frame">
                <div class="up_file" id="up_file">
                    <input type="file" class="fl  h_20 w_300" id="file" multiple=multiple name="upload" onchange="fileSelected()" />
                    <input type="submit" value="上传" />
                </div>
                <iframe name=‘hidden_frame‘ id="hidden_frame" style=‘display:none‘></iframe>
                <div id="fileInfo"></div>
            </form>
//上传后的回调方法
        function callback(msg) {
            document.getElementById("file").outerHTML = document.getElementById("file").outerHTML;
            $(‘#attr_url‘).val(msg);
            document.getElementById(‘fileInfo‘).innerHTML +="<p>上传成功</p>"
        }

后台:

case "upload":
                    #region 上传文件
                    {
                        string path = "";
                        if (context.Request.Files.Count > 0)
                        {
                            HttpFileCollection files = context.Request.Files;//接受文件
                            if (files.Count > 0)
                            {
                                foreach (string i in files)
                                {
                                    HttpPostedFile file = files[i];
                                    string fileName = Path.GetFileName(file.FileName);//获取文件名
                                    string fileExt = Path.GetExtension(fileName);//获取文件类型
                                    if (!Directory.Exists(context.Server.MapPath("/emailFiles/")))
                                    {
                                        Directory.CreateDirectory(context.Server.MapPath("/emailFiles/"));
                                    }
                                    
                                    string dircStr = "/emailFiles/" + emp.uid + "/";
                                    if (!Directory.Exists(context.Server.MapPath(dircStr)))
                                    {
                                        Directory.CreateDirectory(Path.GetDirectoryName(context.Server.MapPath(dircStr)));
                                    }
                                    string name =dircStr+ DateTime.Now.Year+"-"+DateTime.Now.Month+"/";
                                    if (!Directory.Exists(context.Server.MapPath(name)))
                                    {
                                        Directory.CreateDirectory(Path.GetDirectoryName(context.Server.MapPath(name)));
                                    }
                                    string fileLoadName = name +DateTime.Now.Day.ToString()+DateTime.Now.Hour.ToString()+DateTime.Now.Minute.ToString()+DateTime.Now.Second.ToString()+ fileExt;
                                    file.SaveAs(context.Server.MapPath(fileLoadName));
                                    path += fileLoadName + ,;
                                }
                            }
                            path = path.TrimEnd(,);
                            context.Response.Write("<script>parent.callback(‘" + path + "‘)</script>");//调用前台的回调方法
                        }
                        break;
                    }
                    #endregion

 

使用input=file上传

标签:

原文地址:http://www.cnblogs.com/Celebrator/p/4650384.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!