Sumit Roy
Sumit Roy

Reputation: 423

An object reference is required for the nonstatic field, method, or property 'masterpage.fileupload'

I am using a static method to upload my image in the project folder but not able to do so as I am getting the error :

An object reference is required for the nonstatic field, method, or property 'inheritedclass.fileupload'

My function is as follows:

[WebMethod]
public static string FileUploadUsingJqueryNew(string nameofimage)
{
    string File_Name = nameofimage;       
    FileUploadID.SaveAs(Server.MapPath("~/UploadFile/" + File_Name));
    imgID.ImageUrl = "../UploadFile/" + File_Name;
    imgID.Visible = true;
    return nameofimage;
}

Upvotes: 0

Views: 2245

Answers (2)

Soner Gönül
Soner Gönül

Reputation: 98750

  • FileName = nameofimage

You can't define a Filename like this. FileName is a property. Use like this String fileName = FileUpload1.FileName; Don't forget the comma!

  • FileUpload.SaveAs()

You can't do it. You can use it with non-static method in FileUpload control. Use like this;

FileUpload f = new FileUpload();
f.SaveAs();

Upvotes: 1

VMAtm
VMAtm

Reputation: 28355

You use FileUpload.SaveAs method of WebControl, which is on your page. Your page is a object with properties, one of them is your FileUpload.

You can't pass a reference for your control to the static method. You can use only non-static method to use your FileUpload control.

Update:
According that you are using [WebMethod] attribute, I think you are trying to pass your file to the Web-Service method. This can be done by adding a FileUpload handler passing contents of your file to the [WebMethod] as Stream using HttpPostedFile.InputStream.

Upvotes: 2

Related Questions