Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net Umbraco v4 vs可爱的AJAX上传控制_Asp.net_Ajax_Asp.net Ajax_Updatepanel_Umbraco - Fatal编程技术网

Asp.net Umbraco v4 vs可爱的AJAX上传控制

Asp.net Umbraco v4 vs可爱的AJAX上传控制,asp.net,ajax,asp.net-ajax,updatepanel,umbraco,Asp.net,Ajax,Asp.net Ajax,Updatepanel,Umbraco,我有一个自定义用户控件,我在Umbraco CMS的页面中使用它。。。自从升级到版本4后,这个用户控件似乎不再工作了 用户控件包含一个ajax uploader控件(此处为support request post:),它允许用户上载图像,然后将上载的图像显示给用户。控件和图像显示包含在UpdatePanel中,这就是问题所在-返回到UpdatePanel的数据似乎无效,因此客户端正在吐出虚拟对象并抛出此错误: Sys.WebForms.PageRequestManagerParserErrorE

我有一个自定义用户控件,我在Umbraco CMS的页面中使用它。。。自从升级到版本4后,这个用户控件似乎不再工作了

用户控件包含一个ajax uploader控件(此处为support request post:),它允许用户上载图像,然后将上载的图像显示给用户。控件和图像显示包含在UpdatePanel中,这就是问题所在-返回到UpdatePanel的数据似乎无效,因此客户端正在吐出虚拟对象并抛出此错误:

Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.

Details: Error parsing near '

<!DOCTYPE html PUBL'.
Sys.WebForms.PageRequestManagerParserErrorException:无法分析从服务器收到的消息。此错误的常见原因是当通过调用response.Write()修改响应、启用响应筛选器、HttpModules或服务器跟踪时。
详细信息:分析“附近”时出错

我知道这个答案并不是直接解决你的问题,更像是一个解决办法。这是因为我不熟悉您使用的自定义控件。 不过,今晚我会看看你的问题,看看是否能为你目前使用的代码和插件找到解决方案

同时,我可能会给你一些关于我自己使用的ajax上传的想法。 我知道这是一大块代码,但如果你感兴趣,你可以试试:)


您体内的html代码:

using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.SessionState;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.IO;
using System.Xml;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.member;

namespace SH.umbServices
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class AjaxFileUpload : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context) 
        {
            string strResponse = "error";
            try 
            {
                //string strFileName = Path.GetFileName(context.Request.Files[0].FileName);
                //string strExtension = Path.GetExtension(context.Request.Files[0].FileName).ToLower();
                //string strSaveLocation = context.Server.MapPath("../images/temp") + "\\" + strFileName;
                //context.Request.Files[0].SaveAs(strSaveLocation);
                UmbracoSave(context);
                strResponse = "success";
            }
            catch
            { 
            }
            context.Response.ContentType = "text/plain";
            context.Response.Write(strResponse);
        }

        public bool IsReusable 
        {
            get 
            {
                return false;
            }
        }

        #region "umbMediaItem"
        protected string UmbracoSave(HttpContext context)
        {
            string mediaPath = "";

            if (context.Request.Files[0] != null)
            {
                if (context.Request.Files[0].FileName != "")
                {
                    // Find filename
                    string _text = context.Request.Files[0].FileName;
                    string _ext = Path.GetExtension(context.Request.Files[0].FileName);
                    string filename;
                    string _fullFilePath;

                    //filename = _text.Substring(_text.LastIndexOf("\\") + 1, _text.Length - _text.LastIndexOf("\\") - 1).ToLower();
                    filename = Path.GetFileName(_text);
                    string _filenameWithoutExtention = filename.Replace(_ext, "");
                    int _p = 1212; // parent node.. -1 for media root)

                    // create the Media Node
                    umbraco.cms.businesslogic.media.Media m = umbraco.cms.businesslogic.media.Media.MakeNew(
                        _filenameWithoutExtention, umbraco.cms.businesslogic.media.MediaType.GetByAlias("image"), User.GetUser(0), _p);

                    // Create a new folder in the /media folder with the name /media/propertyid
                    System.IO.Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath(umbraco.GlobalSettings.Path + "/../media/" + m.Id.ToString()));
                    _fullFilePath = System.Web.HttpContext.Current.Server.MapPath(umbraco.GlobalSettings.Path + "/../media/" + m.Id.ToString() + "/" + filename);
                    context.Request.Files[0].SaveAs(_fullFilePath);

                    // Save extension
                    //string orgExt = ((string)_text.Substring(_text.LastIndexOf(".") + 1, _text.Length - _text.LastIndexOf(".") - 1));
                    string orgExt = Path.GetExtension(context.Request.Files[0].FileName).ToLower();
                    orgExt = orgExt.Trim(char.Parse("."));
                    try
                    {
                        m.getProperty("umbracoExtension").Value = orgExt;
                    }
                    catch { }

                    // Save file size
                    try
                    {
                        System.IO.FileInfo fi = new FileInfo(_fullFilePath);
                        m.getProperty("umbracoBytes").Value = fi.Length.ToString();
                    }
                    catch { }

                    // Check if image and then get sizes, make thumb and update database
                    if (",jpeg,jpg,gif,bmp,png,tiff,tif,".IndexOf("," + orgExt + ",") > 0)
                    {
                        int fileWidth;
                        int fileHeight;

                        FileStream fs = new FileStream(_fullFilePath,
                            FileMode.Open, FileAccess.Read, FileShare.Read);

                        System.Drawing.Image image = System.Drawing.Image.FromStream(fs);
                        fileWidth = image.Width;
                        fileHeight = image.Height;
                        fs.Close();
                        try
                        {
                            m.getProperty("umbracoWidth").Value = fileWidth.ToString();
                            m.getProperty("umbracoHeight").Value = fileHeight.ToString();
                        }
                        catch { }

                        // Generate thumbnails
                        string fileNameThumb = _fullFilePath.Replace("." + orgExt, "_thumb");
                        generateThumbnail(image, 100, fileWidth, fileHeight, _fullFilePath, orgExt, fileNameThumb + ".jpg");

                        image.Dispose();
                    }
                    mediaPath = "/media/" + m.Id.ToString() + "/" + filename;

                    m.getProperty("umbracoFile").Value = mediaPath;
                    m.XmlGenerate(new XmlDocument());

                    Member mbr = Member.GetCurrentMember();
                    umbraco.cms.businesslogic.property.Property avt = mbr.getProperty("memberAvatar");
                    avt.Value = m.Id;
                    mbr.XmlGenerate(new XmlDocument());
                    mbr.Save();
                    //string commerceFileName = mediaPath;
                    //CommerceSave(commerceFileName);
                }
            }
            return mediaPath;
        }

        protected void generateThumbnail(System.Drawing.Image image, int maxWidthHeight, int fileWidth, int fileHeight, string fullFilePath, string ext, string thumbnailFileName)
        {
            // Generate thumbnail
            float fx = (float)fileWidth / (float)maxWidthHeight;
            float fy = (float)fileHeight / (float)maxWidthHeight;
            // must fit in thumbnail size
            float f = Math.Max(fx, fy); //if (f < 1) f = 1;
            int widthTh = (int)Math.Round((float)fileWidth / f); int heightTh = (int)Math.Round((float)fileHeight / f);

            // fixes for empty width or height
            if (widthTh == 0)
                widthTh = 1;
            if (heightTh == 0)
                heightTh = 1;

            // Create new image with best quality settings
            Bitmap bp = new Bitmap(widthTh, heightTh);
            Graphics g = Graphics.FromImage(bp);
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;

            // Copy the old image to the new and resized
            Rectangle rect = new Rectangle(0, 0, widthTh, heightTh);
            g.DrawImage(image, rect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);

            // Copy metadata
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
            ImageCodecInfo codec = null;
            for (int i = 0; i < codecs.Length; i++)
            {
                if (codecs[i].MimeType.Equals("image/jpeg"))
                    codec = codecs[i];
            }

            // Set compresion ratio to 90%
            EncoderParameters ep = new EncoderParameters();
            ep.Param[0] = new EncoderParameter(Encoder.Quality, 90L);

            // Save the new image
            bp.Save(thumbnailFileName, codec, ep);
            bp.Dispose();
            g.Dispose();

        }
        #endregion
    }
}
Upload AvatarUpload new
jQuery-ajaxupload插件:(/scripts/jQuery.ajaxupload.js) ' "

handler.ashx:(存储在/umbraco/AjaxFileUpload.ashx中)

使用系统;
使用系统集合;
使用系统数据;
使用System.Web;
使用System.Web.SessionState;
使用System.Web.Services;
使用System.Web.Services.Protocols;
使用System.IO;
使用System.Xml;
使用系统图;
使用System.Drawing.Drawing2D;
使用系统、绘图、成像;
使用umbraco.BusinessLogic;
使用umbraco.cms.businesslogic.member;
命名空间SH.umbServices
{
/// 
///$codebehindclassname的摘要说明$
/// 
[WebService(命名空间=”http://tempuri.org/")]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
公共类AjaxFileUpload:IHttpHandler,iRequiresessionState
{
公共void ProcessRequest(HttpContext上下文)
{
字符串strResponse=“error”;
尝试
{
//字符串strFileName=Path.GetFileName(context.Request.Files[0].FileName);
//string strExtension=Path.GetExtension(context.Request.Files[0].FileName.ToLower();
//字符串strSaveLocation=context.Server.MapPath(“../images/temp”)+“\\”+strFileName;
//context.Request.Files[0].SaveAs(strSaveLocation);
UmbracoSave(上下文);
strResponse=“成功”;
}
抓住
{ 
}
context.Response.ContentType=“text/plain”;
context.Response.Write(strResponse);
}
公共布尔可重用
{
得到
{
返回false;
}
}
#区域“媒体项目”
受保护的字符串UmbracoSave(HttpContext上下文)
{
字符串mediaPath=“”;
if(context.Request.Files[0]!=null)
{
if(context.Request.Files[0].FileName!=“”)
{
//查找文件名
字符串_text=context.Request.Files[0]。文件名;
字符串_ext=Path.GetExtension(context.Request.Files[0].FileName);
字符串文件名;
字符串_fullFilePath;
//filename=\u text.Substring(\u text.LastIndexOf(\\”)+1,\u text.Length-\u text.LastIndexOf(\\”)-1.ToLower();
filename=Path.GetFileName(_text);
字符串_filenameWithoutExtention=filename.Replace(_ext,“”);
int _p=1212;//父节点..-1表示媒体根)
//创建媒体节点
umbraco.cms.businesslogic.media.media m=umbraco.cms.businesslogic.media.media.MakeNew(
_不带扩展名的文件名,umbraco.cms.businesslogic.media.MediaType.GetByAlias(“图像”),User.GetUser(0),\u p);
//在/media文件夹中创建一个名为/media/propertyid的新文件夹
System.IO.Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath(umbraco.GlobalSettings.Path+“/../media/”+m.Id.ToString());
_fullFilePath=System.Web.HttpContext.Current.Server.MapPath(umbraco.GlobalSettings.Path+“/../media/”+m.Id.ToString()+“/”+filename);
context.Request.Files[0].SaveAs(\u fullFilePath);
//保存扩展名
//字符串orgExt=((字符串)_text.Substring(_text.LastIndexOf(“.”+1,_text.Length-_text.LastIndexOf(“.”-1));
字符串orgExt=Path.GetExtension(context.Request.Files[0].FileName).ToLower();
orgExt=orgExt.Trim(字符解析(“.”);
尝试
{
m、 getProperty(“umbracoExtension”).Value=orgExt;
}
捕获{}
//保存文件大小
尝试
{
System.IO.FileInfo fi=新文件信息(_fullFilePath);
m、 getProperty(“umbracopytes”).Value=fi.Length.ToString();
}
捕获{}
//检查图像是否正确,然后获取尺寸,制作拇指并更新数据库
如果(“,jpeg,jpg,gif,bmp,png,tiff,tif,”.IndexOf(“,“+orgExt+”,”)大于0)
{
int文件宽度;
int文件高度;
FileStream fs=新FileStream(\u fullFilePath,
FileMode.O
<div id="container"><h2>Upload Avatar</h2><button class="button" id="submitChangeAvatar" type="button">Upload new</button></div>
using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.SessionState;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.IO;
using System.Xml;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.member;

namespace SH.umbServices
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class AjaxFileUpload : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context) 
        {
            string strResponse = "error";
            try 
            {
                //string strFileName = Path.GetFileName(context.Request.Files[0].FileName);
                //string strExtension = Path.GetExtension(context.Request.Files[0].FileName).ToLower();
                //string strSaveLocation = context.Server.MapPath("../images/temp") + "\\" + strFileName;
                //context.Request.Files[0].SaveAs(strSaveLocation);
                UmbracoSave(context);
                strResponse = "success";
            }
            catch
            { 
            }
            context.Response.ContentType = "text/plain";
            context.Response.Write(strResponse);
        }

        public bool IsReusable 
        {
            get 
            {
                return false;
            }
        }

        #region "umbMediaItem"
        protected string UmbracoSave(HttpContext context)
        {
            string mediaPath = "";

            if (context.Request.Files[0] != null)
            {
                if (context.Request.Files[0].FileName != "")
                {
                    // Find filename
                    string _text = context.Request.Files[0].FileName;
                    string _ext = Path.GetExtension(context.Request.Files[0].FileName);
                    string filename;
                    string _fullFilePath;

                    //filename = _text.Substring(_text.LastIndexOf("\\") + 1, _text.Length - _text.LastIndexOf("\\") - 1).ToLower();
                    filename = Path.GetFileName(_text);
                    string _filenameWithoutExtention = filename.Replace(_ext, "");
                    int _p = 1212; // parent node.. -1 for media root)

                    // create the Media Node
                    umbraco.cms.businesslogic.media.Media m = umbraco.cms.businesslogic.media.Media.MakeNew(
                        _filenameWithoutExtention, umbraco.cms.businesslogic.media.MediaType.GetByAlias("image"), User.GetUser(0), _p);

                    // Create a new folder in the /media folder with the name /media/propertyid
                    System.IO.Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath(umbraco.GlobalSettings.Path + "/../media/" + m.Id.ToString()));
                    _fullFilePath = System.Web.HttpContext.Current.Server.MapPath(umbraco.GlobalSettings.Path + "/../media/" + m.Id.ToString() + "/" + filename);
                    context.Request.Files[0].SaveAs(_fullFilePath);

                    // Save extension
                    //string orgExt = ((string)_text.Substring(_text.LastIndexOf(".") + 1, _text.Length - _text.LastIndexOf(".") - 1));
                    string orgExt = Path.GetExtension(context.Request.Files[0].FileName).ToLower();
                    orgExt = orgExt.Trim(char.Parse("."));
                    try
                    {
                        m.getProperty("umbracoExtension").Value = orgExt;
                    }
                    catch { }

                    // Save file size
                    try
                    {
                        System.IO.FileInfo fi = new FileInfo(_fullFilePath);
                        m.getProperty("umbracoBytes").Value = fi.Length.ToString();
                    }
                    catch { }

                    // Check if image and then get sizes, make thumb and update database
                    if (",jpeg,jpg,gif,bmp,png,tiff,tif,".IndexOf("," + orgExt + ",") > 0)
                    {
                        int fileWidth;
                        int fileHeight;

                        FileStream fs = new FileStream(_fullFilePath,
                            FileMode.Open, FileAccess.Read, FileShare.Read);

                        System.Drawing.Image image = System.Drawing.Image.FromStream(fs);
                        fileWidth = image.Width;
                        fileHeight = image.Height;
                        fs.Close();
                        try
                        {
                            m.getProperty("umbracoWidth").Value = fileWidth.ToString();
                            m.getProperty("umbracoHeight").Value = fileHeight.ToString();
                        }
                        catch { }

                        // Generate thumbnails
                        string fileNameThumb = _fullFilePath.Replace("." + orgExt, "_thumb");
                        generateThumbnail(image, 100, fileWidth, fileHeight, _fullFilePath, orgExt, fileNameThumb + ".jpg");

                        image.Dispose();
                    }
                    mediaPath = "/media/" + m.Id.ToString() + "/" + filename;

                    m.getProperty("umbracoFile").Value = mediaPath;
                    m.XmlGenerate(new XmlDocument());

                    Member mbr = Member.GetCurrentMember();
                    umbraco.cms.businesslogic.property.Property avt = mbr.getProperty("memberAvatar");
                    avt.Value = m.Id;
                    mbr.XmlGenerate(new XmlDocument());
                    mbr.Save();
                    //string commerceFileName = mediaPath;
                    //CommerceSave(commerceFileName);
                }
            }
            return mediaPath;
        }

        protected void generateThumbnail(System.Drawing.Image image, int maxWidthHeight, int fileWidth, int fileHeight, string fullFilePath, string ext, string thumbnailFileName)
        {
            // Generate thumbnail
            float fx = (float)fileWidth / (float)maxWidthHeight;
            float fy = (float)fileHeight / (float)maxWidthHeight;
            // must fit in thumbnail size
            float f = Math.Max(fx, fy); //if (f < 1) f = 1;
            int widthTh = (int)Math.Round((float)fileWidth / f); int heightTh = (int)Math.Round((float)fileHeight / f);

            // fixes for empty width or height
            if (widthTh == 0)
                widthTh = 1;
            if (heightTh == 0)
                heightTh = 1;

            // Create new image with best quality settings
            Bitmap bp = new Bitmap(widthTh, heightTh);
            Graphics g = Graphics.FromImage(bp);
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;

            // Copy the old image to the new and resized
            Rectangle rect = new Rectangle(0, 0, widthTh, heightTh);
            g.DrawImage(image, rect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);

            // Copy metadata
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
            ImageCodecInfo codec = null;
            for (int i = 0; i < codecs.Length; i++)
            {
                if (codecs[i].MimeType.Equals("image/jpeg"))
                    codec = codecs[i];
            }

            // Set compresion ratio to 90%
            EncoderParameters ep = new EncoderParameters();
            ep.Param[0] = new EncoderParameter(Encoder.Quality, 90L);

            // Save the new image
            bp.Save(thumbnailFileName, codec, ep);
            bp.Dispose();
            g.Dispose();

        }
        #endregion
    }
}