Exemple #1
0
        protected void MyFile_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
        {
            //validate file
            if (!MyFile.HasFile)
            {
                lblError.Text = "Please select a picture file to upload.";
                return;
            }

            //Grab the file name from its fully qualified path at client
            String strFileFullPathName = MyFile.PostedFile.FileName;

            // only the attched file name not its path
            String strFileName = System.IO.Path.GetFileName(strFileFullPathName);
            String strFileExt  = System.IO.Path.GetExtension(strFileName);

            switch (strFileExt.ToUpper())
            {
            case ".PNG":
            case ".JPG":
            case ".JPEG":
            case ".JP2":
            case ".TIF":
            case ".TIFF":
            case ".GIF":
            case ".BMP":
                break;

            default:
                lblError.Text = "Sorry, the file you selected is not a known image type.";
                return;
            }
            if (strFileName.IndexOf("'") >= 0 | strFileName.IndexOf("\"") >= 0)
            {
                lblError.Text = "Sorry, the file name is not valid. Please remove spacial characters (e.g. qoutes, double quotes) from the file name and try again.";
                return;
            }

            System.Diagnostics.Debug.WriteLine("Document ext: " + strFileExt);

            //Calculating Save File name
            //Tablename_ObjectID_RunningNumber
            String baseFileName = TableName + "_" + KeyValue;

            if (!ServerImagePath.EndsWith(@"\"))
            {
                ServerImagePath += @"\";
            }
            if (!HttpImagePath.EndsWith("/"))
            {
                HttpImagePath += "/";
            }
            String saveFileName         = baseFileName + strFileExt;
            String saveFileNameFullPath = ServerImagePath + saveFileName;
            int    fileRunning          = 0;

            while (System.IO.File.Exists(saveFileNameFullPath))
            {
                fileRunning++;
                saveFileName         = baseFileName + "-" + fileRunning.ToString() + strFileExt;
                saveFileNameFullPath = ServerImagePath + saveFileName;
            }

            //Update data table
            OleDbConnection Conn             = new OleDbConnection(ConfigurationManager.ConnectionStrings[ConnectionName].ConnectionString);
            String          strUpdateCommand = String.Format("UPDATE [{0}] SET [{1}] = '{2}' WHERE [{3}] = {4}{5}{4}", TableName, ImageField, System.IO.Path.GetFileName(saveFileName), KeyField, KeyFieldType == KeyFieldTypeEnum.String ? "'" : "", KeyValue);
            OleDbCommand    objCommand       = new OleDbCommand(strUpdateCommand, Conn);

            objCommand.Connection.Open();
            objCommand.ExecuteNonQuery();
            System.Diagnostics.Debug.WriteLine("Record updated");

            //Save uploaded file to server
            try
            {
                //Upload the file
                MyFile.SaveAs(saveFileNameFullPath);
                lblMessage.Text  = "Your File was Uploaded Sucessfully.";
                CurrentImageFile = System.IO.Path.GetFileName(saveFileName);
            }
            catch (Exception Exp)
            {
                lblError.Text = "OOPS! The server has encountered an error. Please check your file and try again.";
            }
            String js = String.Format("$(\"{0}\").attr(\"src\",\"{1}\")", imgCurrentPhoto.ClientID, getImgTag(HttpImagePath, CurrentImageFile, true));

            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, UpdatePanel1.GetType(), "newimg", js, true);
            ajaxlabel.Text = js;
            UpdatePanel1.Update();
        }
Exemple #2
0
    protected void MyFile_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
    {
        Session["uploadError"]   = String.Empty;
        Session["uploadMessage"] = String.Empty;
        //validate file
        if (e.State != AjaxControlToolkit.AsyncFileUploadState.Success || !MyFile.HasFile)
        {
            //lblError.Text = "Please select a picture file to upload.";
            Session["uploadError"] = "Please select a picture file to upload.";
            return;
        }

        //Grab the file name from its fully qualified path at client
        String strFileFullPathName = MyFile.PostedFile.FileName;

        // only the attched file name not its path
        String strFileName = System.IO.Path.GetFileName(strFileFullPathName);
        String strFileExt  = System.IO.Path.GetExtension(strFileName);

        switch (strFileExt.ToUpper())
        {
        case ".PNG":
        case ".JPG":
        case ".JPEG":
        case ".JP2":
        case ".TIF":
        case ".TIFF":
        case ".GIF":
        case ".BMP":
            break;

        default:
            //lblError.Text = "Sorry, the file you selected is not a known image type.";
            Session["uploadError"] = "Sorry, the file you selected is not a known image type.";

            return;
        }
        if (strFileName.IndexOf("'") >= 0 | strFileName.IndexOf("\"") >= 0)
        {
            //lblError.Text = "Sorry, the file name is not valid. Please remove spacial characters (e.g. qoutes, double quotes) from the file name and try again.";
            Session["uploadError"] = "Sorry, the file name is not valid. Please remove spacial characters (e.g. qoutes, double quotes) from the file name and try again.";
            return;
        }

        System.Diagnostics.Debug.WriteLine("Document ext: " + strFileExt);

        //Calculating Save File name
        //Tablename_ObjectID_RunningNumber
        String baseFileName = TableName + "_" + KeyValue;

        if (!ServerImagePath.EndsWith(@"\"))
        {
            ServerImagePath += @"\";
        }
        if (!HttpImagePath.EndsWith("/"))
        {
            HttpImagePath += "/";
        }
        String saveFileName         = baseFileName + strFileExt;
        String saveFileNameFullPath = ServerImagePath + saveFileName;
        int    fileRunning          = 0;

        while (System.IO.File.Exists(saveFileNameFullPath))
        {
            fileRunning++;
            saveFileName         = baseFileName + "-" + fileRunning.ToString() + strFileExt;
            saveFileNameFullPath = ServerImagePath + saveFileName;
        }

        //Update data table
        OleDbConnection Conn             = new OleDbConnection(ConfigurationManager.ConnectionStrings[ConnectionName].ConnectionString);
        String          strUpdateCommand = String.Format("UPDATE [{0}] SET [{1}] = '{2}' WHERE [{3}] = {4}{5}{4}", TableName, ImageField, System.IO.Path.GetFileName(saveFileName), KeyField, KeyFieldType == KeyFieldTypeEnum.String ? "'" : "", KeyValue);
        OleDbCommand    objCommand       = new OleDbCommand(strUpdateCommand, Conn);

        objCommand.Connection.Open();
        objCommand.ExecuteNonQuery();
        System.Diagnostics.Debug.WriteLine("Record updated");

        //Save uploaded file to server
        try
        {
            //Upload the file
            MyFile.SaveAs(saveFileNameFullPath);
            Session["uploadMessage"] = "Your File was Uploaded Successfully.";
            CurrentImageFile         = System.IO.Path.GetFileName(saveFileName);
            if (!String.IsNullOrWhiteSpace(WebConfigReducedImagePath))
            {
                //saveFileNameFullPath = ServerImagePath + saveFileName;
                ImageUtil.resizeImage_caller(saveFileName, WebConfigImagePath, WebConfigReducedImagePath, reducedImageMaxWidth, reducedImageMaxHeight);
            }
        }
        catch (Exception Exp)
        {
            //lblError.Text = "OOPS! The server has encountered an error. Please check your file and try again.";
            Session["uploadError"] = "OOPS! The server has encountered an error. Please check your file and try again.";
        }

        /*
         *   update to controls on the page will not work as MyFile_UploadedComplete event is fired in an iframe.
         *
         *   put any update you want to do in the Button1_click below
         *
         */
    }