2

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

用户控件包含一个ajax上传控件(支持请求发布在这里:http ://cutesoft.net/forums/53732/ShowThread.aspx#53732 ),它允许用户上传图片,然后将上传的图片显示给用户。控件和图像显示包含在 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'.

我认为这与导致此问题的 Umbraco v4 中母版页的实现方式有关。关于为什么会发生这种情况的任何想法,以及我可以尝试解决的问题?

仅供参考,这是一篇描述错误及其可能原因的博客文章: http ://weblogs.asp.net/leftslipper/archive/2007/02/26/sys-webforms-pagerequestmanagerparsererrorexception-what-it-is-and-how -to-avoid-it.aspx

我在 updatePanel 中执行任何 Response.write 或 Response.Redirect 我没有使用任何响应过滤器 我禁用了服务器跟踪 我没有使用 response.transfer

但是,不管上述情况如何,在 Umbraco v3 站点上使用相同的用户控件可以正常工作,这让我相信它与导致这种情况的 v4 有关。

任何建议都非常感谢

4

2 回答 2

1

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

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


我的例子

我在自己的网站上有一个上传控件,它与 umbraco 完美配合。 表单和上传由 jQuery 提供支持(上传由 jQuery.AjaxUpload 插件处理)

我在我的 umbraco 文件夹中创建了一个通用处理程序,用于处理服务器上的文件。 在媒体库中为它创建一个媒体项目(在我的情况下,在您的个人资料页面上作为头像上传器,它还将新创建的媒体项目添加到成员的头像属性中)

jQuery 代码:(存储在页面头部的脚本块中,或单独的脚本中)

initializeChangeAvatarForm = function() {
    var button = $('#submitChangeAvatar'), interval;
    new AjaxUpload(button,{
        action: '/umbraco/AjaxFileUpload.ashx',
        name: 'myfile',
        onSubmit : function(file, ext){
            // change button text to uploading + add class (with animating background loading image)
            button.text('Uploading').addClass('loading');
            // If you want to allow uploading only 1 file at time,
            // you can disable upload button
            this.disable();
        },
        onComplete: function(file, response){
            button.text('Upload nieuw').removeClass('loading');
            // Although plugins emulates hover effect automatically,
            // it doens't work when button is disabled
            button.removeClass('hover');
            window.clearInterval(interval);
            // enable upload button
            this.enable();
        }
    });
};

$(document).ready(function(){
    initializeChangeMailForm();
});

html代码在你的身体:

<div id="container"><h2>Upload Avatar</h2><button class="button" id="submitChangeAvatar" type="button">Upload new</button></div>

jQuery ajaxupload 插件:(/scripts/jQuery.ajaxupload.js) '因为这段代码太长,所以我直接添加了一个指向我使用的 .js 文件的链接 '以及另一个指向我获得插件的页面的链接

处理程序 .ashx:(存储在 /umbraco/AjaxFileUpload.ashx 中)

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
    }
}

我希望这对我使用的东西有所帮助。

于 2009-07-15T13:38:44.813 回答
0

Pfew,..这是很多代码。

我只对在 Umbraco 中保存媒体感兴趣,但也很高兴看到 jQuery 上传。

你使用什么上传 jQuery lib?我找到了几个。

您可以通过使用 Path.Combine、FileInfo.Extension 合并 if 和一些额外的变量来简化代码。但是,嘿,我有 Resharper 让我的生活更轻松。

于 2009-08-13T12:10:24.717 回答