1

I have created an MVC ASP.Net application and hosted in within IIS. Part of my app allows for users to upload a csv file. The data then needs to be read out of the csv and into an object to be used elsewhere.

The problem I have is when hosted in IIS the file can not be read. Here is my code:

            using (StreamReader CsvReader = new StreamReader(filePath))
            {
                MessageHandler.NewNote("Opened CSV Reader");
                string inputLine = "";
                int lineNumber = 0;     
                while ((inputLine = CsvReader.ReadLine()) != null && !string.IsNullOrEmpty(inputLine))
                {
                    //Do something here
                }
            }

Can someone please advise how I get this working. The filepath is generated using the following upload form:

    <div id="UploadForm">
        <% using (Html.BeginForm("FileUpload", "ImportExport", Model,
                FormMethod.Post, new { enctype = "multipart/form-data" }))
    {%>
        <input name="uploadFile" type="file" accept=".csv" style="width:70%;" />
        <input type="submit" value="Upload File" />               
        <%} %>
    </div>
2
  • 1
    What do you mean "cannot be read"? Is there an error? How does it not perform as expected? You need to provide this information up front, rather than us needing to ask you for it. Commented Apr 20, 2015 at 16:24
  • Error was File not found. See below answer. Commented Apr 21, 2015 at 12:07

1 Answer 1

1

The filePath is coming from the client, there should be no file existing on the server at the given file path.

You need to read the data from the Stream coming from the upload form instead of the file path.

Please refer to the following link

http://msdn.microsoft.com/en-us/library/system.web.httprequest.inputstream.aspx

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

1 Comment

perfect exactly what I was looking for.

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.