2

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,

3 Answers 3

2

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);

    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Geek. I will try this. However, how does this get called the same way through the action attribute on the form?
sorry for the late reply.Yes,No input parameter required in web service method.you have to made only one change in aspx add runat="server" tag in form section.
0

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.

2 Comments

I will update my question with the current state of my web method. It is similar to the link. But i was just trying to read in the file and break point on it. However, I never hit the break point nor does firebug show that any call was made to the service.
I would at least think it would report service not found.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.