Skip to main content
Inspiring
September 10, 2010
Question

INSERT RECORD & UPLOAD FILE WITH ONE BUTTON

  • September 10, 2010
  • 1 reply
  • 2145 views

I'm developing an ASP page that collects data from users and drops it into a database on the serer.  One of the fields on the form will be an "attachment" which could be a .docx, jpeg, etc...

I'm able to insert the record to the database and right the filename and path using the "file" field.  I've also added a second form on the page which allows the user to select the same file again to upload it to the server.


I find two problems with this method immediatly:  1) Users should only have to select the file once and click on submit once 2) The path&file should be saved with the new location of the file for future retrieval and not the path&file of where it came from.

I've searched and tried using the FREE ASP Uploads but can't get it to work by saving the data to the database and uploading the data at the same time.

It seems that I need to be able to perform two actions from one submit button.

I'd greatly appreciate some tips/tricks/suggestions on how to do this if someone has done this before.  I've spent 3 days reading and reading and testing and testing with no results.  I'm not a pure asp programmer so I was hoping Dreamweaver had something built in but it seems that it is only for Cold Fusion pages.

Thanks in advance for any help.

This topic has been closed for replies.

1 reply

Lon_Winters
Inspiring
September 12, 2010

I just checked your other thread on this and saw that you asked a question about sharing a script - sorry for not replying in that thread. I don't have any scripts I can share, as I said I have only the commercial extensions. One is for ASP and I have another for PHP. If you'd like more info on these PM me and I give you the details.  They do what you're looking for plus more - inserting the file name and location into the DB, uploading the file to a predetermined folder or creating a folder on the fly (based on a logged in user), progress bar, generate an email, etc etc. So if you've spent three days on this without success, then it may be a matter on what your time is worth!

But for now, what free scripts have you tried? Are you able to upload the file but just not in the same step as the insert? I may be able to help you get that working - and there is no reason it cant be accomplish with the same form submission.

Inspiring
September 13, 2010

Lon -

Thanks for your continued effort to help me through this.  I have been trying to use Upload.asp written by Jacob Gilley and ASPUpload.  I can get them to work fine, I am just struggling to make everything connect on the insert record where I have the rest of the data I've collected from the users.

Here's the form I've asked the user to fill out.  I would really like to make the "Attachment" field the file field and be able to browse the file, upload the file, and return to the form with the file information (title & Path) saved in this field.  The plan is to keep all the files being uploaded in the same directory so the path should be hard coded.  The users will then be able to update form or view it later and need to be able to retrieve the uploaded file.

I know how to restrict the file size and types of file to upload with the upload snippets of code I have already.  I just can't get it all together as you can tell, I tried to use a upload file "checkbox" to upload the files first before filling out the form which seems very non-user-friendly.

Lon_Winters
Inspiring
September 16, 2010

I think you're getting close - you've gotten past a couple majot hurdels, one being getting the upload script to work, even if it's by itself.

The key is, what does the script provide for an action, when you're using it by itself, as an action when the upload completes - a redirect perhaps? If so,then here's one possible solutions;

Place the upload field and insert fields in the same form, with only one button. So when the form is submitted that will execute both actions. You'll want the upload come before the insert, then when it's complete, leave out the redirect or other end action and let the code move on to the insert script. The upload has to come first because it needs the path to retrieve the file. When the insert is complete, then it will re-direct.

Then here's the other catch - before the insert, you're going to have to strip everything but the filename from the field before the insert record, assign the result to a variable and replace the form field in the insert script with your variable.  The stripping part, you''have to google that up, that's one thing I let my extensions handle. But it would be something like this (but only in concept, not syntax)

myFileUploadVariable = Request.Form("myFileUploadField") minus everything but the filename, or everything after the last "/" or however it's done.

Once you get our variable set up and populated with just the filename, then set up your insert as usual, and locate the line of code for that Request.Form field, it will look something like this:

 MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param2", 201, 1, 255, Request.Form("myFileUploadField")) ' adLongVarChar

Then just replace the form field with your variable -

MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param2", 201, 1, 255, myFileUploadVariable) ' adLongVarChar

You could skip all this, and just insert the whole path, and do the stripping later when you go to reference it for a download.

So let me know what you think and if this method works for you, there are a couple options at least - but this is the most basic and can be built on.