473,394 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

C# Web App: Simple file uploading/retrieiving system saving to a SQL database

55
Can anybody help me with a simple file uploading and retrieving idea?

What I need is a simple system that uploads the files to be stored in a SQL database which are then shown on the front end in a datagrid, with this datagrid there needs to be a view button where the user can view the file when they return to the page.

I tried playing with the OpenFileDialog class but to no avail.

Any help will be much appreciated thanks :)
Nov 26 '08 #1
5 2763
Curtis Rutland
3,256 Expert 2GB
OpenFileDialog won't work on the web. The only way to upload files on the web (at least in ASP.NET) is using the FileUpload control, which isn't very flexible. It just brings up the browser's select file dialog, and you can't tell it what path to start in or anything.

So you need to use this with a button, and when the user clicks a button, you need to save the file, at least temporarily. Then, you need to use a System.IO.FileStream to get a byte array of the file. Then, insert that into a database.

Then you can create your gridview, with one column containing links. And on the page it links to, maybe use a querystring to pass the pk value, then select the file out of the DB and use the Response.WriteFile method to present it.

Now, if you really want to have some "fun" with the uploading, you could use a silverlight frontend. But you'd have to write a WCF or ASMX webservice to do the dirty work.

Hope that helps.
Nov 26 '08 #2
dmj07
55
Ah i see what you mean so use the FileBytes of the file upload control to return the bytes content of the uploaded file and then store this in the database I presume as a varbinary(MAX)...

Do you have any example of its use with the FileStream as I have never really come across this before?!?

Many thanks for your help on this :)
Nov 26 '08 #3
Curtis Rutland
3,256 Expert 2GB
Actually, I was thinking of using the Image column type, but varbinary should work too. The Image type is the same as a BLOB.

Here's some sample code on getting a byte[] from a file using the FileStream object:
Expand|Select|Wrap|Line Numbers
  1. //c#
  2. FileStream fs = new FileStream(@"c:\dev\ascii.txt", FileMode.Open, FileAccess.Read);
  3. byte[] buffer = new byte[fs.Length];
  4. fs.Read(buffer, 0, (int)fs.Length);
  5.  
Remember, this is in the System.IO namespace.
Nov 26 '08 #4
Curtis Rutland
3,256 Expert 2GB
Although, if you are using the FileBytes method, you don't need to use a filestream...you already have the byte[] from the file.

Just use a parameterized insert statement to insert it into your DB.
Nov 26 '08 #5
dmj07
55
hey many thanks for your help on this one you've been great :)

i'll give it a shot in my program and if all goes well will post some sample code
Nov 27 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Alper Adatoz | last post by:
Hi, i have a little problem. i hope u guys give me a clear solution (: db: mssql i just want to put jpeg file to the image field at the mssql db. and after that i want to call it back..
3
by: James | last post by:
We are writing a web based application which primarily will serve information to run an ivr. Based on our current IVR getting 1000000 hits a week, this is going to be heavy duty. We have very...
6
by: Vijay | last post by:
I need to generate HTML files based on the some codes. One HTML file per code. I have the link (ex:http://123.234.345.456/WebPages/GetTestData.aspx?SomeCode=25), by passing the code as parameter I...
4
by: R Reyes | last post by:
I am trying to code a file uploader (for forum/email attachments) from the client computer to a remote web server via the PUT method (since POST is not allowed ). However, the upload works ONLY...
5
by: snicks | last post by:
I'm trying to exec a program external to my ASP.NET app using the following code. The external app is a VB.NET application. Dim sPPTOut As String sPPTOut = MDEPDirStr + sID + ".ppt" Dim p As...
12
by: SAL | last post by:
Hello, Is it possible to read a CSV from the Client, and bind my Datagrid to the data in the CSV file without uploading the file to the Server first? I have tried and in Debug mode on my...
2
by: Matt | last post by:
I'm new to C#, so this really easy to answer for anyone that is adept at this... I'm having trouble saving data from my text boxes on a user form to my SQL database. Everything works fine while...
10
by: Redhairs | last post by:
In a web farm environment, how to store the user uploading files for future access? Store them in db, local file system or centralized file server? If trying to storing the file in local file...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.