将处理后的图像上传到服务器

时间:2012-09-13 08:38:53

标签: c# javascript ajax

我正在使用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而不使用上传它工作正常,但如果我使用上传控件它不会工作,我会得到任何帮助的错误消息

1 个答案:

答案 0 :(得分: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;
}