JQuery之replace以及给控件赋值

<input type="hidden" name="ImgUrl"  readonly="readonly">
<input type="file" id="imgurl_uploadify" />
<img id="imgurl_view" width="100" height="100"/>

 最上面的那个input是自己添加的,主要用于获取上传的图片的路径,然后传给name为ImgUrl的表单元素,从而在提交的时候将其值传递,下面的两个控件是uploadify上传图片控件要用的

下面是jquery上传代码:

@section scripts{
    <script type="text/javascript">
        $(function () {
            //上传图片
            $("#imgurl_uploadify").uploadify({
                'queueSizeLimit': 1,	//上传的数量
                'uploader': '/HttpHandler/UploadifyHandler.ashx',       //网站中的UploadifyHandler.ashx文件地址
                'swf': '/Content/js/plugins/uploadify/uploadify.swf',   //该文件在项目中的位置
                'onUploadSuccess': function (file, data, response) {    //这里的data是从UploadifyHandler.ashx文件获取过来的netPath
                    //下面的这句话是将上传的图片的url赋值给img控件的src属性
                    $('#imgurl_view').attr('src', data);
                    //这句话是将上传的图片的url赋值给input控件的value属性,从而提交表单的时候会获取到该name为ImgUrl的input控件的value值,这里的WebUrl是Http://localhost:2551
                    $("input[name='ImgUrl']").attr('value', data.replace('@System.Configuration.ConfigurationManager.AppSettings["WebUrl"]', ''));
                },
                'onQueueComplete': function () {
                },
                //检测FLASH失败调用
                'onFallback': function () {
                    alert("您未安装FLASH控件,无法上传图片!请安装FLASH控件后再试。");
                },
                //载入时触发,将flash设置到最小
                'onInit': function () {
                    $(".uploadify-queue").hide();
                }
            });

        })
    </script>
}

 下面再看看图片上传时的路径:

public void ProcessRequest(HttpContext context)
        {
            HttpPostedFile file = context.Request.Files["Filedata"];

//带net的路径变量都是网络路径;带upload的路径变量都是本地服务器路径,即保存到本地的路径
//WebUrl=http://localhost:2551,显示图片的时候是找此路径的 var netRoot = System.Configuration.ConfigurationManager.AppSettings["WebUrl"]; //WebPath=D:项目-----------公司--代码Linkin2Linkin.Web,是将其上传到Web项目中,如果不加这句的是默认保存到Manager里了 var uploadRoot = System.Configuration.ConfigurationManager.AppSettings["WebPath"]; var strogeFolder = "/Upload/Uploadify"; var netPath = string.Format("{1}/{0}/", DateTime.Now.ToString("yyyy/MM/dd"), strogeFolder); //下面这种写法是保存到linkin.Manager项目下的文件夹下了,但是现在要保存到Web下 //var uploadPath = HttpContext.Current.Server.MapPath(netPath); var uploadPath = string.Format("{0}/{1}/{2}/", uploadRoot, strogeFolder, DateTime.Now.ToString("yyyy/MM/dd")); if (file != null) { if (!Directory.Exists(uploadPath)) { Directory.CreateDirectory(uploadPath); } var fileName = string.Format("{0}{1}", IDHelper.Id32, file.FileName.Substring(file.FileName.IndexOf('.'))); netPath = string.Format("{2}{0}{1}", netPath, fileName, netRoot); uploadPath = string.Format("{0}{1}", uploadPath, fileName); file.SaveAs(uploadPath); //下面这句代码缺少的话,上传成功后上传队列的显示不会自动消失 context.Response.Write(netPath); } else { context.Response.Write("0"); } }

            

原文地址:https://www.cnblogs.com/cocoon/p/4997021.html