473,412 Members | 2,306 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,412 software developers and data experts.

downloading files in .NET urgent

1
How can i download files like(.doc,.txt,.pdf,.gif) which are already uploaded to databse in asp.net using c#,plz help me regarding this
Jul 19 '07 #1
2 925
kenobewan
4,871 Expert 4TB
Welcome to TSDN. Please explain and provide more info. Thanks.
Jul 19 '07 #2
RoninZA
78
Hello rajnow...

I am assuming that you want to download a file that is stored in a table in a database, in, for example, an image column...

The solution is pretty intricate, downloading the file in chunks, but see code below (this uses the SQLHelper class, but you could quite comfortable write code to replace that as well (variables in $-signs need to be provided):
Expand|Select|Wrap|Line Numbers
  1. System.Data.SqlClient.SqlConnection cn = null;
  2. int offset = 0;
  3.  
  4. System.Data.SqlClient.SqlParameter[] pointerParams = 
  5.     new System.Data.SqlClient.SqlParameter[2];
  6.  
  7. string selectSql = @"
  8.     SELECT    @pointer = TEXTPTR(" + $fieldName$ + @"),
  9.         @length = DataLength(" + $fieldName$ + @")
  10.     FROM    " + $tableName$ + @"
  11.     WHERE    " + $whereClause$;
  12.  
  13. pointerParams[0] = new System.Data.SqlClient.SqlParameter("@pointer", SqlDbType.Binary, 16);
  14. pointerParams[1] = new System.Data.SqlClient.SqlParameter("@length", SqlDbType.Int, 4);
  15. pointerParams[0].Direction = ParameterDirection.Output;
  16. pointerParams[1].Direction = ParameterDirection.Output;
  17.  
  18. DAL.SqlHelper.ExecuteNonQuery(
  19.     $connectionString$, 
  20.     CommandType.Text, 
  21.     selectSql, 
  22.     pointerParams);
  23.  
  24. cn = new System.Data.SqlClient.SqlConnection($connectionString$);
  25. cn.Open();
  26.  
  27. if (cn.State != ConnectionState.Open)
  28.     return null;
  29.  
  30. System.Data.SqlClient.SqlCommand cmdRead = new System.Data.SqlClient.SqlCommand(
  31.     "READTEXT " + $tableName$ + "." + $fieldName$ + @" @Pointer @Offset @Size HOLDLOCK", cn);
  32.  
  33. System.Data.SqlClient.SqlParameter pointerParam = cmdRead.Parameters.Add("@Pointer", SqlDbType.Binary, 16);
  34. System.Data.SqlClient.SqlParameter offsetParam = cmdRead.Parameters.Add("@Offset", SqlDbType.Int);
  35. System.Data.SqlClient.SqlParameter sizeParam = cmdRead.Parameters.Add("@Size", SqlDbType.Int);
  36. pointerParam.Value = pointerParams[0].Value;
  37. offsetParam.Value = 0;
  38.  
  39. System.Data.IDataReader reader;
  40.  
  41. byte[] buffer = new byte[System.Convert.ToInt32(pointerParams[1].Value)];
  42. do
  43. {
  44.     pointerParam.Value = pointerParams[0].Value;
  45.     if ((offset + $BufferChunkLength$) >= System.Convert.ToInt32(pointerParams[1].Value))
  46.         sizeParam.Value = System.Convert.ToInt32(pointerParams[1].Value) - offset;
  47.     else 
  48.         sizeParam.Value = $BufferChunkLength$;
  49.  
  50.     reader = cmdRead.ExecuteReader(CommandBehavior.SingleResult);
  51.     reader.Read();
  52.     reader.GetBytes(0, 0, buffer, offset, System.Convert.ToInt32(sizeParam.Value));
  53.     reader.Close();
  54.     offset += System.Convert.ToInt32(sizeParam.Value);
  55.     offsetParam.Value = offset;
  56.  
  57. } while (offset < Convert.ToInt32(pointerParams[1].Value));
  58.  
  59. return buffer;
  60.  
Jul 19 '07 #3

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

Similar topics

1
by: m004202002 | last post by:
I have recently visit a website where to download a file ,I need to login then by clicking http://www.abc.com/jj/Download.php?sID=e0992e5916 the file abc.exe start downloading . How to code...
4
by: Luke StClair | last post by:
Only marginally belonging in this newsgroup... but oh well. I've just started writing in python, and I want to make the files available on the web. So I did the standard <a...
0
by: sales | last post by:
If you are having difficulty downloading large files, please check out www.Downloads4Dialups.com. Mention this newsgroup in the "Special Instructions" window on the Shipping Form, and receive a...
2
by: Bala | last post by:
Hi I am trying to download the PDF files from my webserver using ASP.Net. All my files are stored at F Drive on webserver. Like this F:\Main Folder\Sub Folder\Files\File1.pdf I am...
1
by: just.starting | last post by:
Hi, My dot net client downloads files and checks for any new files time to time. The server is apache2.0.53 server. So what happens is that my file download thing works fine if I dont try to call...
3
by: just.starting | last post by:
Hi, My dot net client downloads files and checks for any new files time to time. The server is apache2.0.53 server. So what happens is that my file download thing works fine if I dont try to call...
0
by: just.starting | last post by:
I am having problem while downloading files from an apache server2.0.53 with php4.3.10.While downloading some files it generally stops after downloading some specific amount and then stops...
8
by: ginnisharma1 | last post by:
Hi All, I am very new to C language and I got really big assignment in my work.I am wondering if anyone can help me.........I need to port compiler from unix to windows and compiler is written...
7
by: Ehsan | last post by:
I foundd this code in ASPN Python Cookbook for downloading files in python but when it finished downloading files the files became corrupted and didn't open, the files in internet havn't any...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.