0

我正在使用 imageFX 处理上传的图像并在处理到我的服务器后将其保存 imageFX 是一个 javascript 库,可将图像转换为已处理的画布我想要做的是获取该画布并使用 AJAX 将其上传到我的服务器但是我收到错误响应,这是我的代码:

<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script src="Scripts/pixastic/pixastic.core.js" type="text/javascript"></script>
<script src="Scripts/pixastic/desaturate.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {

        $("#save").click(function () {

            var image = document.getElementById("filterimage").toDataURL("image/png");
            image = image.replace('data:image/png;base64,', '');
            alert(image);
            $.ajax({
                type: 'POST',
                url: "ImageFxExample.aspx/UploadImage",
                data: '{ "imageData" : "' + image + '" }',
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function (msg) {
                    alert('Image saved successfully !');
                },
                error: function () {
                    alert("error!!");
                }
            });
        });

    });
   </script>
 <script type="text/javascript">

     function strfilter(filter) {
         var img = document.getElementById("<%=filterimage.ClientID %>");
         Pixastic.process(img, filter);
         document.getElementById("save").style.display = "block";
     }
   </script>

标记:

    <asp:FileUpload ID="fileUpload" runat="server"   />
    <asp:Button ID="upload_btn" runat="server" onclick="upload_btn_Click" Text="upload" />
    <asp:Image ID="filterimage" runat="server" ImageUrl="" />

服务器端 :

protected void upload_btn_Click(object sender, EventArgs e){
    string folderPath = "UploadedImage/";
    string imageName = fileUpload.FileName;
    string saveLocation = Server.MapPath(folderPath) + imageName;
    fileUpload.SaveAs(saveLocation);
    filterimage.ImageUrl = folderPath + imageName;
}

[WebMethod()]
public static void UploadImage(string imageData){
    string Path = "UploadedImage/";
    string folderPath = HttpContext.Current.Server.MapPath(Path);
    string fileNameWitPath = folderPath + DateTime.Now.ToString().Replace("/", "-").Replace(" ", "- ").Replace(":", "") + ".png";

    FileStream fs = new FileStream(fileNameWitPath, FileMode.Create);
    BinaryWriter bw = new BinaryWriter(fs);

    byte[] data = Convert.FromBase64String(imageData);

    bw.Write(data);
    bw.Close();
}

如果我在不使用上传的情况下为 filterimage 放置一个简单的 imageUrl,它可以正常工作,但是如果我使用上传控件,它将无法正常工作,并且我会收到错误消息,请提供任何帮助

4

1 回答 1

0

我找到了正确的答案,它正在将图像转换为'png' b4 你上传它

protected void upload_btn_Click(object sender, EventArgs e)
{
    string folderPath = "UploadedImage/";
    string  imageName = fileUpload.FileName;

    string saveLocation = Server.MapPath(folderPath);
    string ext = Path.GetExtension(imageName);
   string name= imageName.Replace(ext, ".png");
   saveLocation = Server.MapPath(folderPath) + name;
    fileUpload.SaveAs(saveLocation);
    filterimage.ImageUrl = folderPath + name;
}
于 2012-09-14T07:01:58.743 回答