Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
C#
[WebMethod]
public static string btnSubmit_Click()
{
    Thread.Sleep(3000);
    //upload your file here
    if (FileUpload2.HasFile)
    {
        try
        {
            string path = Server.MapPath(".");
            FileUpload2.SaveAs(path + "\\new\\" + FileUpload2.FileName);
        }
        catch (Exception ex)
        {
        }
    }
    else
    {
    }
}

I am trying to upload a file and showing the uploaded file simultaneously and facing the error like "An object reference is required for the non-static field, method, or property 'parent.FileUpload2'”
Posted
Updated 5-Jul-13 3:54am
v2
Comments
PRAKASH9 5-Jul-13 9:05am    
please make your method non-static instead of static.may be solved your problem

You cannot access instance members from static members so you have 2 choices.

1. Make the method an instance method (remove the static keyword)

2. Make the field (i.e. FileUpload2) a static (add the static keyword)

The one you choose will depend on whether the field should be shared across all instances or not.

More information about static :

http://www.dotnetperls.com/static-field[^]

UPDATE

HTML

<input type="file" name="file_upload" id="file_upload" />


Javascript

C#
$(function() {
    $("#file_upload").uploadify({
        'fileSizeLimit' : '50KB',
        'swf'           : '/uploadify/uploadify.swf',
        'uploader'      : '/uploadify/uploadify.php',
        'onSelectError' : function() {
            alert('The file ' + file.name + ' returned an error and was not added to the queue.');
        }
    });
});


For more info check this :

http://www.uploadify.com/documentation/uploadify/onselecterror/[^]

UPDATE 2

C#
onError: function (event, queueId, fileObj, errorObj) {
                $("#msg").html(fileObj.name + " was not uploaded ");
                if (errorObj.status == 404)
                    $("#msg").html("Could not find upload script. Use a path relative to: " + "<?= getcwd() ?>");
                else if (errorObj.type === "HTTP")
                    $("#msg").html("error " + errorObj.type + ": " + errorObj.status);
                else if (errorObj.type === "File Size")
                    $("#msg").html(fileObj.name + " " + errorObj.type + " Limit: " + errorObj.info / 1000 + " KB");
                else
                    $("#msg").html("error " + errorObj.type + ": " + errorObj.text);
            }


For more info :

http://stackoverflow.com/questions/11577868/mvc-3-uploadify-how-to-show-custom-error-message[^]

Note : For this You can get error codes from the link I put UPDATE section.

I hope this will help to you.
 
Share this answer
 
v3
Comments
chetna2810 6-Jul-13 1:40am    
Thanks for reply..
actually i am new to jquery trying to upload a image in asp.net using jquery. Jquery always call a static method and i am unable to use fileupload control in the static method. So, i cant't remove static keyword..Suggest me if you have some idea on this..
Thanks in advance..
chetna2810 6-Jul-13 8:32am    
yup :) i found my solution using uploadify pulgin but..can you tell me how can i show my desire msg when file size in increased.(using fileSizeLimit of uploadify pulgin)
Sampath Lokuge 6-Jul-13 8:53am    
@cheetu2810 Plz check my UPDATE section.
chetna2810 6-Jul-13 9:00am    
thnx :) i did that my desired message in coming but still the default message is coming like "some files were not added to the queue.." :(
Sampath Lokuge 6-Jul-13 9:12am    
@cheetu2810 try with UPDATE 2 section.
Try this

C#
public  string btnSubmit_Click()
{
    Thread.Sleep(3000);
    //upload your file here
    if (FileUpload2.HasFile)
    {
        try
        {
            string path = Server.MapPath(".");
            FileUpload2.SaveAs(path + "\\new\\" + FileUpload2.FileName);
        }
        catch (Exception ex)
        {
        }
    }
    else
    {
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900