pghtech
pghtech

Reputation: 3702

Having problems using Forms input to upload file to web service

So I am using ASP.NET 2.0 and trying to use a simple Form to upload a file to a web service.

I have the action attrib set to the url of my web service. However, in firefox, I can't see that it is making any call to that service at all.
NOTE: I can throw int the below "Action" value to a browser minus the name of the web method and get a page showing the available web method so I believe the URL for the "Action" attribute is correct.

<form id="fileUpload" action="http://localhost/AcmeABC/services/FileUploadService.asmx/ImportRates" method="post" enctype="multipart/form-data">
<input type="text" id="fileName"  name="fileName" />
<asp:FileUpload runat="server" id="fileArray"/>
<input type="submit" value="Submit" /> 

[WebService(Namespace = "http://www.abc.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
[ScriptService]
public class FileUploadService : System.Web.Services.WebService {

    [WebMethod]
    public void ImportRates(string fileName, byte[] fileArray) {
        try {
            MemoryStream memoryStream = new MemoryStream(fileArray);
        }
        catch (Exception ex) {
            string error = string.Format("Error thrown for file {0} with {1} error.", fileName, ex);
        }
    }

How can I see what is going on since I don't see any call be made.

I am also of the opinion that this might not be the best approach. I am new to the entire web development space so I am trying to find better ways of handling problems. Please advice any other approach that might be recommended for uploading a file to a web method.

Thanks,

Upvotes: 2

Views: 3230

Answers (3)

santosh singh
santosh singh

Reputation: 28642

The problem is in reading the binary data as a parameter input. you needed to be read the data from HttpContext input stream

Modify your webservice method as follows

[WebMethod]
public void ImportRates()
{
    try
    {


        //HTTP Context to get access to the submitted data
        HttpContext postedContext = HttpContext.Current;
        //File Collection that was submitted with posted data
        HttpFileCollection files = postedContext.Request.Files;
        //Make sure a file was posted
        string fileName =files[0].FileName;

        MemoryStream memoryStream = new MemoryStream(files[0].InputStream);


    }
    catch (Exception ex1)
    {
        string error = string.Format("Error thrown for file {0} with {1} error.", fileName, ex);

    }
}

Upvotes: 2

SquidScareMe
SquidScareMe

Reputation: 3218

I just noticed that there is no port specified in the action url. This could be perfectly fine depending on your development environment but it's worth mentioning.

Upvotes: 0

SquidScareMe
SquidScareMe

Reputation: 3218

Can you show us the code in ImportRates web method? Does it look like the web method here?

Another option to consider besides a web service for file upload is an ASHX handler. You can think of an ashx handler as an asp.net codebehind file without the view. Generally speaking ashx handlers are considered more lightweight than web services.

Upvotes: 0

Related Questions