Esempio n. 1
0
    // ajax请求发送给服务器一个图像文件,要求暂存到Session中,供后面的sendtext请求使用
    // chat.aspx?action=sendimage
    void DoSendImage()
    {
        string strError = "";
        int nRet = 0;

        if (this.Request.Files.Count == 0)
        {
            strError = "请求中没有包含图像文件";
            goto ERROR1;
        }

        try
        {

            DeleteTempFile();

            HttpPostedFile file = this.Request.Files[0];

            string strContentType = file.ContentType;
            string strImage = StringUtil.GetFirstPartPath(ref strContentType);
            if (strImage != "image")
            {
                strError = "只允许上传图像文件";
                goto ERROR1;
            }

            sessioninfo.PostedFileInfo = new PostedFileInfo();
            sessioninfo.PostedFileInfo.FileName = PathUtil.MergePath(sessioninfo.GetTempDir(), "postedfile" + Path.GetExtension(file.FileName));
            //file.SaveAs(sessioninfo.PostedFileInfo.FileName + ".tmp");

            //bool bWrited = false;
            using (Stream target = File.Create(sessioninfo.PostedFileInfo.FileName))
            {

                // 缩小尺寸
                nRet = GraphicsUtil.ShrinkPic(file.InputStream,
                        file.ContentType,
                        app.ChatRooms.PicMaxWidth,
                        app.ChatRooms.PicMaxHeight,
                        true,
                        target,
                        out strError);
                if (nRet == -1)
                    goto ERROR1;
                if (nRet == 0)  // 没有必要缩放
                {
                    if (file.InputStream.CanSeek == true)
                        file.InputStream.Seek(0, SeekOrigin.Begin); // 2012/5/20
                    StreamUtil.DumpStream(file.InputStream, target);
                    // bWrited = false;
                }


            }

            /*
            if (bWrited == false)
                File.Copy(sessioninfo.PostedFileInfo.FileName + ".tmp",
                    sessioninfo.PostedFileInfo.FileName,
                    true);
             * */

        }
        catch (Exception ex)
        {
            strError = "ShrinkPic error -" + ex.Message;
            goto ERROR1;
        }

        this.Response.Write("{\"result\":\"OK\"}");
        this.Response.End();
        return;
    ERROR1:
        // app.WriteErrorLog("DoSendImage() error :" + strError);

        this.Response.Charset = "utf-8";
        // this.Response.StatusCode = 500;
        // this.Response.StatusDescription = strError;
        SendImageResultInfo result_info = new SendImageResultInfo();
        result_info.error = strError;
        this.Response.Write(GetResultString(result_info));
        this.Response.End();
    }
Esempio n. 2
0
    // ajax请求发送给服务器一个图像文件,要求暂存到Session中,供后面的sendtext请求使用
    // chat.aspx?action=sendimage
    void DoSendImage()
    {
        string strError = "";
        int    nRet     = 0;

        if (this.Request.Files.Count == 0)
        {
            strError = "请求中没有包含图像文件";
            goto ERROR1;
        }

        try
        {
            DeleteTempFile();

            HttpPostedFile file = this.Request.Files[0];

            string strContentType = file.ContentType;
            string strImage       = StringUtil.GetFirstPartPath(ref strContentType);
            if (strImage != "image")
            {
                strError = "只允许上传图像文件";
                goto ERROR1;
            }

            sessioninfo.PostedFileInfo          = new PostedFileInfo();
            sessioninfo.PostedFileInfo.FileName = PathUtil.MergePath(sessioninfo.GetTempDir(), "postedfile" + Path.GetExtension(file.FileName));
            //file.SaveAs(sessioninfo.PostedFileInfo.FileName + ".tmp");

            //bool bWrited = false;
            using (Stream target = File.Create(sessioninfo.PostedFileInfo.FileName))
            {
                // 缩小尺寸
                nRet = GraphicsUtil.ShrinkPic(file.InputStream,
                                              file.ContentType,
                                              app.ChatRooms.PicMaxWidth,
                                              app.ChatRooms.PicMaxHeight,
                                              true,
                                              target,
                                              out strError);
                if (nRet == -1)
                {
                    goto ERROR1;
                }
                if (nRet == 0)  // 没有必要缩放
                {
                    if (file.InputStream.CanSeek == true)
                    {
                        file.InputStream.Seek(0, SeekOrigin.Begin); // 2012/5/20
                    }
                    StreamUtil.DumpStream(file.InputStream, target);
                    // bWrited = false;
                }
            }

            /*
             * if (bWrited == false)
             *  File.Copy(sessioninfo.PostedFileInfo.FileName + ".tmp",
             *      sessioninfo.PostedFileInfo.FileName,
             *      true);
             * */
        }
        catch (Exception ex)
        {
            strError = "ShrinkPic error -" + ex.Message;
            goto ERROR1;
        }

        this.Response.Write("{\"result\":\"OK\"}");
        this.Response.End();
        return;

ERROR1:
        // app.WriteErrorLog("DoSendImage() error :" + strError);

        this.Response.Charset = "utf-8";
        // this.Response.StatusCode = 500;
        // this.Response.StatusDescription = strError;
        SendImageResultInfo result_info = new SendImageResultInfo();

        result_info.error = strError;
        this.Response.Write(GetResultString(result_info));
        this.Response.End();
    }