Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File not being uploaded but also not receiving any errors

Status
Not open for further replies.

dougancil

IS-IT--Management
Mar 31, 2009
44
US
I have a site that I've almost completed and while testing it today noticed that it's not uploading any files or doing the bulk insert in SQL that I need it to. Here is my codebehind:

Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Submit1_Click(ByVal sender As Object, ByVal e As EventArgs)

If Not File1.PostedFile Is Nothing And File1.PostedFile.ContentLength > 0 Then
Dim fn As String = System.IO.Path.GetFileName(File1.PostedFile.FileName)
Dim SaveLocation As String = Server.MapPath("Data") & "\" & fn
Try
File1.PostedFile.SaveAs(SaveLocation)
Response.Write(<center>Thank you for your submission.</center>)

' get the file that was submitted and check if it was .txt file
Dim theFile As FileInfo = New FileInfo(SaveLocation)
If theFile.Extension <> ".txt" Then
Response.Write(<center>Please submit a text file.</center>)
End If

Dim importPath As String = Server.MapPath("Data") & "\upload.txt"
If File.Exists(importPath) Then
' do something with existing upload.txt file, maybe archive?
End If

' rename the uploaded file to upload.txt for importing
theFile.MoveTo(importPath)

' and bulk import the data:
Dim connection As String = ConfigurationManager.ConnectionStrings("Dialerresults").ConnectionString
Dim results As New DataTable

Using con As New SqlConnection(connection)
con.Open()

' execute the bulk import
Using cmd As SqlCommand = con.CreateCommand
cmd.CommandText = "bulk insert dialerresults from '" & importPath & "' " & _
" with ( fieldterminator = ',', rowterminator = '\n' )"
cmd.ExecuteNonQuery()
End Using
End Using

Catch Exc As Exception
Response.Write("Error: " & Exc.Message)
End Try
Else
Response.Write(<center>Please select a file to upload.</center>)
End If

End Sub
End Class

Can anyone see why the file is not being uploaded? I'm sure its something simple but I've been working on this for a while and may have missed something. Any assistance would be welcome.

Thank you,

Doug
 
2 things.
1. you should log the exception, not display the error message to the user. log everything about the exception, not just the message. use the ToString() property to get everything.
3. don't use Response.Write within a webform. Response.Write is a low level operation that should do not need direct access to. Instead place a label on the form and set it's text property to the message you want to display.

also consider separating the operations into unique methods or objects. this will also help narrow down where the error is occurring. I would create an object to save/archive/move the uploaded file and another object to import the file into the database. something like
Code:
try
{
   var datafile = new Archiver().Archive(File1.PostedFile.FileName);
   new DataImporter().Import(datafile);
   StatusLabel.Text = "Imported Successfully";
}
catch(Exception e)
{
      LogManager.GetLogger(GetType()).Error(e.Message, e);
      StatusLabel.Text = "Oops, there was a problem.";
}
Archiver & DataImporter are objects you create to process the data. LogManager is part of the log4net library.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Jason,

Thank you for your response but the reason that I'm displaying the error to the user is that this is going to be used by medical offices and they are known for just sending e mails stating "this web page/link/whatever is broken or not working." The error message I'm giving them is so that when I get a message from them, I can determine the cause of the problem quicker rather than me constantly checking the logs of the server to determine if the file uploaded correctly. Lastly, at this point, the file is being accepted by the server and not sending me errors but when a file is uploaded to the server, I am able to look in both the destination folder and the sql server table and there is no data there. What I'm trying to figure out at this point is why the page isn't giving me an error or why the file isn't being uploaded to the server. That's a bigger question for me at the moment. I can always go back and tweek the interface later.
 
you are loosing detail about exceptions by only displaying the message and not the entire exception. the message is useless without the stack trace.

if the file is saved to the server and the sql command runs without error then you're page is working correct. have you verified the uploaded file is not empty?

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
I have verified that both the uploaded file and the sql table are both empty.
 
I just tried to upload a file that was not in the correct format (the page is only supposed to accept .txt files and I tried to upload a .pdf) and I'm not getting any return errors for that either.
 
if the uploaded file is empty then there won't be any records to import. the user input is the problem.

selecting files other than txt. that's a different issue. there is probably some js you can use to limit the file selection, but you still cannot trust that information on the server. you will need to validate the text file on the server as well.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Jason, it appears that the problem is related to UpdatePanel that I have put File1 within. I have to look at that for the answer.
 
yep, that would do it. ajax and file uploads do not play nice together. I have seen a few examples of ajax-like file uploaders, they usually work with an iframe rather than a true ajax request. I looked into this once for my apps, but decided against it.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Is there an easy workaround for that kind of issue? I don't want to go back and have to re-write my code to get that to work.
 
remove the update panel.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Can you identify that to me? I had that pointed it out to me by someone else but they didn't identify where that was.
 
I don't understand the question.

if you want an ajax file uploader search the web using the phrase "ajax file uploader". chances are the solution is http based, not webforms based, so be ready to do some work with javascript and html.

if you don't want to use an iframe then you will not be using ajax to upload the file. in either case you will want to remove the update panel. (they don't provide any value anyway).

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Jason,

What I'm asking is ... where is the updatepanel? I was told that the reason that my files weren't uploading is because my "file1" is within an updatepanel. I've looked through my code and don't see where that's located. I'd gladly remove the code if I knew where it was.
 
The update panel is a webforms UI control. it would be located somewhere within the markup of a webform, master page or webusercontrol. there is also the possibility the update panel is created dynamically, but that would be a nightmare in itself.

How do you know you are using an update panel if you don't know where it is?

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
I was told this by another developer (via another forum) but as I cant find it, I'm now assuming that that may not be the cause.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top