Pergunta

I am uploading image to imgur.com using API on button click but I want to avoid the page refresh on button click, I tried disabling onClick to false and manually call function via jquery but unfortunately I was unable to run that, is there any other method to upload without postback?

Second Question: We can't call simple method to static. Any trick to call other method in static?

Foi útil?

Solução

I think this will help you :

Here is the code to upload '.jpg' Files

Script Code :

<script src="JS/jquery-2.1.0.js"></script>
<script type="text/javascript">
    function SaveAllDetails() {
        if (document.getElementById("FileUploadMyImage").value != "") {
            var file = document.getElementById('FileUploadMyImage').files[0];
            var reader = new FileReader();
            reader.readAsDataURL(file);
            reader.onload = UpdateFiles;
        }
        else {
            alert('Please Choose An Image');
        }
    }

    function UpdateFiles(evt) {
        var result = evt.target.result;
        var ImageSave = result.replace("data:image/jpeg;base64,", "");
        var savobject = { 'savingvalues': ImageSave };
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "Default.aspx/SaveAllDetails",
            data: JSON.stringify(savobject),
            dataType: "json",
            success: function () {
                alert('SuccessFully Uploaded');
            },
            error: function () {
                alert('Not Uploaded');
            }
        });
    }
</script>

Html Part :

<div>
<input type="file" id="FileUploadMyImage" />
    <input type="button" value="Submit" onclick="SaveAllDetails();" />
</div>

Code behind your asp.net form :

[WebMethod(EnableSession = true)]
public static void SaveAllDetails(string savingvalues)
{
    byte[] getImageData = Convert.FromBase64String(savingvalues);
}

the variable 'getImageData' is your image and you can send it directly to your 'Stored Procedure'

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top