Record the ASP. NET upload code

Source: Internet
Author: User

 

Click to upload

Code:
  1. Protected void imgbtnup_click (Object sender, imageclickeventargs E)
  2. {
  3. // String Path = fileupload1.postedfile. filename;
  4. // String imgname = path. substring (path. lastindexof ("//") + 1 );
  5. // String serverpath = server. mappath ("Dropbox/") + imgname;
  6. // Fileupload1.postedfile. saveas (serverpath );
  7. Try
  8. {
  9. If (fileupload1.postedfile. filename! = "")
  10. {
  11. String filepath = fileupload1.postedfile. filename; // retrieve the index of the last "." In the file name (inclusive path ).
  12. String filenames = filepath. substring (filepath. lastindexof ("//") + 1 );
  13. String fileextend = filepath. substring (filepath. lastindexof (".") + 1 );
  14. String filesize = fileupload1.postedfile. contentlength. tostring ();
  15. If (! (Fileextend = "xls" | fileextend = "xls" | fileextend = "Doc" | fileextend = "Doc" | fileextend = "TXT" | | fileextend = "TXT" | fileextend = "RAR" | fileextend = "RAR "))
  16. {
  17. Bll. Pub. Show (this, "the file format is not supported. Please pack the file to be uploaded! ");;
  18. Return;
  19. }
  20. String serverpath = server. mappath ("Dropbox/") + filenames;
  21. Fileupload1.postedfile. saveas (serverpath );
  22. Mdropbox. upname = filenames;
  23. Mdropbox. upsize = convert. toint32 (filesize );
  24. Mdropbox. uptime = datetime. now;
  25. Mdropbox. upurl = serverpath;
  26. // Mdropbox. uid
  27. Bdropbox. Add (mdropbox );
  28. Bll. Pub. Show (this, "the file is uploaded successfully! ");
  29. }
  30. }
  31. Catch
  32. {
  33. Bll. Pub. Show (this, "add error! ");
  34. }
  35. }

 

Explanation: fileupload1 upload Control name

Row 33 calls public methods and has previously written student.csdn.net/space.php

27 rows add method:

Code:
  1. /// <Summary>
  2. /// Add a data entry
  3. /// </Summary>
  4. Public int add (model. tbdropbox Model)
  5. {
  6. Int rowsaffected;
  7. Sqlparameter [] parameters = {
  8. New sqlparameter ("@ upid", sqldbtype. Int, 4 ),
  9. New sqlparameter ("@ upname", sqldbtype. nvarchar, 100 ),
  10. New sqlparameter ("@ uptime", sqldbtype. datetime ),
  11. New sqlparameter ("@ upurl", sqldbtype. nvarchar, 200 ),
  12. New sqlparameter ("@ uid", sqldbtype. Int, 4 ),
  13. New sqlparameter ("@ upsize", sqldbtype. Int, 4 )};
  14. Parameters [0]. Direction = parameterdirection. output;
  15. Parameters [1]. value = model. upname;
  16. Parameters [2]. value = model. uptime;
  17. Parameters [3]. value = model. upurl;
  18. Parameters [4]. value = model. uid;
  19. Parameters [5]. value = model. upsize;
  20. Dbhelpersql. runprocedure ("tbdropbox_add", parameters, out rowsaffected );
  21. Return (INT) parameters [0]. value;
  22. }

 

 

 

21 rows of "tbdropbox_add" stored in SQL

Code:
  1. Create procedure [DBO]. [tbdropbox_add]
  2. @ Upid int output,
  3. @ Upname nvarchar (100 ),
  4. @ Uptime datetime,
  5. @ Upurl nvarchar (200 ),
  6. @ Uid int,
  7. @ Upsize int
  8. As
  9. Insert into [tbdropbox] (
  10. [Upname], [uptime], [upurl], [uid], [upsize]
  11. ) Values (
  12. @ Upname, @ uptime, @ upurl, @ uid, @ upsize
  13. )
  14. Set @ upid =@@ identity
  15. Go

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.