Answered by:
Reading File Using Web Service

Question
-
User1655374113 posted
hello,
I want to read a file using web service. I m unable to write the web service. Can anyone tell me how to start.
I search in google but I didnt find any web service reading a file.
Thanks
Tuesday, March 2, 2010 10:15 AM
Answers
-
User-1878433365 posted
Your webservice is composed of an .asmx file and an .cs file but everything happens in the .cs file so you need to work on your method to do what I mentioned:
public string VerifyCsvFile(string csvFile) { string result = ""; StreamReader r; string fullPath = MapPath(csvFile); try { r = new StreamReader(fullPath); }
My suggestion now is try to read and process the file on your regular .aspx, .cs pages first. And just after you feel comfortable with that move your code to the webservice.- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, March 3, 2010 9:14 AM
All replies
-
User-1878433365 posted
Hello!
Can you give me a little bit more information about your project? Think about your web service... supposse it has one method... do you need to call that method to do what? To read a file remotely and then deliver that file to you? Is that what you need?
Tuesday, March 2, 2010 10:52 AM -
User1655374113 posted
hi,
I m uploading a text file. After uploading file to the server, I want to read the text file line by line. I want to show error message, if there is error in any line of that text file. if there are no errors, save the file into the database.
Here, I want to create a web service for reading the text file line by line.
Thanks for ur reply..
Tuesday, March 2, 2010 10:57 AM -
User-1878433365 posted
I see,
Well, here's some code I use to read a csv text file line by line to verify that each row has exactly 18 elements before processing the file. If the function returns an empty string everything is ok, if it returns something else, then that is the error:
private static string VerifyCsvFile(string csvFile) { string result = ""; StreamReader r; try { r = new StreamReader(csvFile); } catch (Exception ex) { return ex.Message; } using (r) { string line; int i = 1; while ((line = r.ReadLine()) != null) { string [] elements = line.Split(','); if (elements.Length != 18) result += "Line " + i + " length = " + elements.Length + "<br>"; } } r.Close(); return result; }
I do not see why do you need this to be a web service, but you can do it of course.Create your .asmx file, for instance FileServices.asmx and put the method definition on your .cs file:
[WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] public class FileServices : System.Web.Services.WebService { [WebMethod] public string VerifyCsvFile(string csvFile) { string result = ""; StreamReader r; try { r = new StreamReader(csvFile); } catch (Exception ex) { return ex.Message; } using (r) { string line; int i = 1; while ((line = r.ReadLine()) != null) { string[] elements = line.Split(','); if (elements.Length != 18) result += "Line " + i + " length = " + elements.Length + "<br>"; } } r.Close(); return result; } }
Tuesday, March 2, 2010 11:51 AM -
User1655374113 posted
Thank you.
I will work on what u tell and I let u know, if I got any doubts or errors
Tuesday, March 2, 2010 12:04 PM -
User1655374113 posted
when I m trying to upload text file, It is uploading and saved in the folder
How can I access that file and open using web service in this scenario..
Tuesday, March 2, 2010 1:14 PM -
User1655374113 posted
How can I access this web service from .cs file?
where we have to give the file path ?
Tuesday, March 2, 2010 1:34 PM -
User-1878433365 posted
Your upload page and your web service are located on the same server so they can both access the physical path of your files.
This is not a typical scenario for a web service. On a typical scenario, the webservice can be located on another server and you application will reside in a different server.
To consume a web service you need to add a web reference in visual studio and that will create you a proxy:
http://msdn.microsoft.com/en-us/library/d9w023sx(VS.80).aspx
And the call will be pretty simple:
private void Call_Web_Service_Method(){ ServerName.WebServiceName CallWebService = new ServerName.WebServiceName(); String sGetValue = CallWebService.MethodName(); Label1.Text = GetValue; }
But I insist... you do not seem to need a web service... everything can be handled on your main application.
Tuesday, March 2, 2010 4:03 PM -
User1655374113 posted
I know, there is no need of web service. But lot off applications are there to access this web service.
So I need it. My problem is, I m unable to read the file in the web service which is in the folder of my machine.
how to give that path in my web service..?
Tuesday, March 2, 2010 4:18 PM -
User1655374113 posted
In your example, U didnt give the path of the file in the web service.
I want that path in the web service. How to access the file from web service?
Tuesday, March 2, 2010 4:20 PM -
User-1878433365 posted
You pass the filename as a parameter to your webservice. It should be a generic method that receives the filename and then reads and processes the file.
You should get the full path of the filename, maybe you need to convert from a 'web path' to a 'physhical path' because to read the file you need the full physical path of the file on the web server.
string physicalPath = MapPath(filename);
You will find the file on the same directory structure where you uploaded.
Wednesday, March 3, 2010 8:52 AM -
User1655374113 posted
We have to give this in web service or .CS file?
Can u explain clearly, wat we have to write in .CS file and how u get that path in web service.
Thanks
Wednesday, March 3, 2010 8:56 AM -
User-1878433365 posted
Your webservice is composed of an .asmx file and an .cs file but everything happens in the .cs file so you need to work on your method to do what I mentioned:
public string VerifyCsvFile(string csvFile) { string result = ""; StreamReader r; string fullPath = MapPath(csvFile); try { r = new StreamReader(fullPath); }
My suggestion now is try to read and process the file on your regular .aspx, .cs pages first. And just after you feel comfortable with that move your code to the webservice.- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, March 3, 2010 9:14 AM -
User1655374113 posted
In the web sevice, It is showing the path.
but it is giving error as..
Cant find part of the path C:\\Documents and setting\\
May I know what is the prob
Wednesday, March 3, 2010 9:45 AM -
User-1878433365 posted
When you do the upload, what is the path that you set for the file? Where in your file system is the uploaded file located?
Where do you have your web application located at? C:\Inetpub\wwwroot\app?
Wednesday, March 3, 2010 3:09 PM -
User330766394 posted
Maybe this can be related to the problem that you mention
http://stackoverflow.com/questions/8996157/how-to-enable-file-system-access-to-web-service
I know its an old topic but maybe somebody else find this usefull
Wednesday, May 29, 2013 9:31 PM