开发者

Asp.net Webforms uploading file using Ajax-Upload

开发者 https://www.devze.com 2023-03-11 21:03 出处:网络
I want to use the Ajax-Upload script to upload multiple images. The kicker is I have to use Asp.net webforms, the way I\'ve been doing ajax has been to have a simple page web method like so.

I want to use the Ajax-Upload script to upload multiple images. The kicker is I have to use Asp.net webforms, the way I've been doing ajax has been to have a simple page web method like so.

[WebMethod()]
public static string Method(string param)
{
    return "Hello World";
}

I am running into trouble figuring out how to upload an image without using the webforms FileUpload control so I can upload files asynchronously. I must be using the wrong search terms because I cannot find another example of someone doing this inside of webforms.

EDIT: To be more specific I am looking for help with the server side. The plugin I linked to handles all of the client side s开发者_如何学JAVAtuff. I am currently looking into writing a customer handler...

I'd prefer to avoid using update panel controls(like the AjaxToolkit) and just stick to the plugin if thats possible.


In asp.net- ajax you can Asyncupload which is part of the Ajax control toolkit. http://www.asp.net/ajax/ajaxcontroltoolkit/samples/AsyncFileUpload/AsyncFileUpload.aspx If you are looking at jquery plugins, take a look at uploadify http://www.uploadify.com.


ANSWER:

What I did was wrote an Image Upload handler script that is pretty basic but it should get the job done. Here it is.

    public void ProcessRequest(HttpContext context)
    {
        string uploadDir = "C:\\Upload";
        try
        {
            Image i = Image.FromStream(context.Request.InputStream);
            string filename = context.Request.Params["qqfile"];

            if (i.RawFormat.Equals(ImageFormat.Png))
            {
                i.Save(uploadDir + "\\" + filename, ImageFormat.Png);
            }
            else if (i.RawFormat.Equals(ImageFormat.Jpeg))
            {
                i.Save(uploadDir + "\\" + filename, ImageFormat.Jpeg);
            }
            else if (i.RawFormat.Equals(ImageFormat.Gif))
            {
                i.Save(uploadDir + "\\" + filename, ImageFormat.Gif);
            }
            else if (i.RawFormat.Equals(ImageFormat.Bmp))
            {
                i.Save(uploadDir + "\\" + filename, ImageFormat.Bmp);
            }
        }
        catch (Exception e)
        {
            context.Response.Write("{'error':'"+e.Message+"'}");
        }

        context.Response.Write("{'success':true}");
    }

And this works with the Ajax-Upload script I linked to earlier. Thanks

0

精彩评论

暂无评论...
验证码 换一张
取 消