ASP.NET Forums 2.0 Modified Highlights (2)

xiaoxiao2021-03-06  78

Today we have to say "how to save the forum accessories directly in the file form, read the exif information of the photo file;" nonsense, go directly to the topic: Preparation: Download the photoproperties class library, used to read EXIF ​​information, Reference this class library 1 in Components, modify the web.config configuration file, add a configuration item UPLOADFIESPATH = "/ upload /" value under Forums / forUMs to save the user uploaded files; 2. Modify the database forums_postatachments The table adds the following field DiskFileName NVARCHAR (256), saving the file name of the attachment on the hard disk Exif Nvarchar (200), using the EXIF ​​information for saving the photo attachment Checkguid NVARCHAR (50) Used to save the detected GUID Note: We modified the final program Will delete the original Content field

Modify the stored procedure:

CREATE Procedure

Forums_postattachment_add (@POSTID INT

, @Userid Int

@Forumid int

, @Filename nvarchar (256

), @Diskfilename nvarchar (256

), @ContentType nvarchar (50

@ContentSize Int

@Exif nvarchar (500

@Checkguid nvarchar (50

))

AS

Begin

IF exists (select postid from forums_postattachments where postid =

@POSTID) RETURN

INSERT INTO

Forums_POSTATTACHMENTS (PostID, ForumID, UserID, [FileName]

, DiskFileName, ContentType, Contentsize, Exif, Checkguid Values

(@Postid, @forumid, @userid, @filename, @diskfilename, @ContentType, @Contentsize, @exif, @Checkguid)

End

Go

Set quoted_identifier off

Go

SET ANSI_NULLS ON

Go

Set quoted_identifier on

Go

SET ANSI_NULLS ON

Go

3. Modify the system configuration management class components under the Configuration / ForumConfiguration.cs file: 1) Add a variable at the head

String UploadFileSpath = "/ upload /";

2) Add the following code in the LoadValeFromConfigurationXML method, read the values ​​in the configuration file:

UploadFilesPath = AttributeCollection ["UPLOADFILESPATH"]. Value; 3) Add UPLOADFILESPATH attribute to the ForumConfiguration class:

Public String UploadFileSpath {Get {Return UploadFileSpath;}}

The configuration file part is completed.

4, modify the Postattachment class under the Components project

Increase the following quote:

Using

System.IO;

Using

JSG.PhotopropertiesLibrary;

Increase the following variables:

String

EXIF;

String

DiskFileName;

String

Checkguid;

Increase the following method:

/ ** /

///

/// Gets the file EXIF ​​information /// /// ///

Private string

GetExif (String ContentType, String FullFileName)

{String imgFilePath = FullFileName; string exif = ""; try {PhotoProperties pp = new PhotoProperties (); pp.Initialize (); pp.Analyze (imgFilePath); if (ContentType.ToLower () == "image / pjpeg") {// Exif Version Exif = "EXIF version:" pp.gettagdatum (36864) .prettyprintValue; // Manufacturer EXIF ​​ = "Manufacturer:" pp.gettagdatum (271) .prettyprintValue; // Model EXIF ​​ = "Model:" pp.gettagdatum (272) .prettyprintValue; // Shooting time EXIF ​​ = "shooting time:" pp.gettagdatum (36868) .prettyprintValue; // Stream transmission time: Exif = "
Time: " pp.gettagdatum (33434) .prettyprintValue " Second "; // Aperture Exif =" Aperture: Pp.getTagDatum (33437) .prettyprintValue; // ISO EXIF ​​ = "ISO:" pp.gettagdatum (34855) .prettyprintValue; // Resolution EXIF ​​ = "resolution:" pp.gettagdatum (282) .prettyprintValue "/" pp.gettagdatum 283) .prettyprintValue;}} catch {} returnif;} The bottom increase of the constructor:

String UploadPath = httpContext.current.server.mappath ("~"

Aspnetforums.configuration.forumConfiguration.getConfig (). UploadFilesPath; UPLOADPATH = "/" Userid.Tostring () "/"; if (!

System.io.directory.exists (UploadPath))

{System.io.directory.createdIRectory (UploadPath);

//Httpcontext.current.user.Identity

DiskFileName = system.guid.newguid (). Tostring ()

System.io.path.Getextension (postedfile.filename); UPLOADPATH =

DiskFileName; postedfile.saveas (UploadPath);

EXIF

=

GetExif (ContentType, UploadPath); Checkguid

=

System.guid.newguid (). TOSTRING ();

Finally, increase 3 properties, the code is as follows:

/ ** /

///

/// disk file name (the physical file name saved on the hard disk) /// 37AE8655-05F8-4ED9-A771-35B41F0222de.ext ///

Public String

DiskFileName

{Get {returnfilename;} set {diskfilename = value;}}

/ ** /

///

/// Photo EXIF ​​information ///

Public String

EXIF

{GET {RETURN EXIF;} set {exif = value;}}

/ ** /

///

/// Used to determine if the user wants to download attachments /// in the form of attachment

Public String

Checkguid

{Get {return checkguid;} set {checkguid = value;}}

5. The following is to modify the SQLDATAPROVIDER project responsible for the system database operation, which is used to complete database operations for the newly added field, and modify the following:

Find public override void addpostattachment (post pos, postattachment attachment) method: //

Increase the DiskFileName field, used for the saved file name on the hard disk, //

Similar to 1990FFB3-3992-4438-B7E2-2BCA963D969F.JPG // This file name is not exposed to end users

MyCommand.Parameters.Add ("@ DiskFileName", Sqldbtype.nvarchar, 256) .value =

Attachment.diskFileName; //

Note Operation to Content //

MyCommand.Parameters.add ("@ Content", Sqldbtype.Image) .value = attachment.content; // Add Photo EXIF ​​information with checkguid fields for verification

MyCommand.Parameters.add ("@ EXIF", Sqldbtype.nvarchar, 500) .value =

Attachment.exif; MyCommand.Parameters.add ("@ checkguid", sqldbtype.nvarchar, 500) .value = attachment.checkguid;

Note: Here you need to comment out of the operation of the Content field, because although the data field is still, but we have deleted the operation of the Content field during the stored procedure;

6. Modify the PopulatePostattachmentFromireAder method in the ForumsDataProvider class under the Components project, complete the conversion from the database to the object, modify the following:

//

Delete the Content field, the attachment content is no longer saved to the database, but is saved directly to the hard disk in the form of the file.

Attachment.content = (Byte []) Reader ["Content"]; //

Increase the DiskFileName field, used for the saved file name on the hard disk, //

Similar to 1990FFB3-3992-4438-B7E2-2BCA963D969F.JPG // This file name is not exposed to end users

Attachment.diskFileName = (String) Reader ["DiskFileName"

] .ToString (); // Add photo EXIF ​​information with checkguid fields used to verify

IF (Reader ["EXIF"]! =

DBNULL.VALUE)

{Attachment.exif = (string) Reader ["exif"];

Attachment.checkguid

= (String) Reader ["Checkguid"]. TOSTRING ();

7, the operation section of this data is already completed, the following is to modify the display section, the system finally displaying the post content is done by the TextPost class under Controls, and the code we want to modify is of course here.

Find the body.text = post.formattedbody in the initializeskin method; in him, the following code is added:

//

Determine if the MIME type of the attachment is a picture, if a picture, then directly call the DisplayImage method to generate the HTML code of the display image // This can expand directly display the accessory as other types of files such as Flash.

String attachment = post.attachmentFileName; if (attachment! = "&& atttachment! = null

)

{AspNetForums.Components.PostAttachment attachment = Posts.GetAttachment (post.PostID); string contentType = attachment.ContentType; if (contentType == "image / pjpeg" || contentType == "image / gif" || contentType == " Image / bmp "| contenttype ==" image / x-png ") {body.text = displayImage (Globals.getsiteURLS (). Postattachment " & guid = " attachment.checkguid); // Display EXIF ​​information if (attachment.exif.length> 0) {body.text = "" attachment.exif "";}}}

The DisplayImage method is very simple, the code is as follows, you can modify the code, add the border to the picture, etc.

/ ** /

///

/// If the attachment is the picture type, then the method is called, generate an HTML code /// /// The image path to display ///

Public String DisplayImage (String

ImageFilePath

{Return " "

8. Modify the accessory download section, the DownloadPostattachment class under the Controls project, the code is as follows:

System.Web.httpContext.current.response.clear (); System.Web.httpContext.current.response.contentType =

Attachment.contentType; //

Determines whether the user submitted from QueryString is the same as the checkguid of the database.

If the same is the same, it is not output in the form of Attachment, and the file directly displays the file (usually providing the GUID value is an attachment attachment) // If it is different, then the output is output in an attachment, directly by the user directly

IF (ForumContext.checkguid! =

Attachment.checkguid)

{System.Web.HttpContext.Current.Response.AppendHeader ( "content-disposition", "attachment; filename =" " System.Web.HttpUtility.UrlEncode (attachment.FileName, System.Text.Encoding.UTF8) " " ");

//System.web.httpContext.current.response.outputstream.write (Attachment.Content, 0, attachment.length);

string diskFileName = System.Web.HttpContext.Current.Server.MapPath ( "~ /" AspNetForums.Configuration.ForumConfiguration.GetConfig (). UploadFilesPath "/" attachment.UserID.ToString () "/"

Attachment.diskFileName); System.Web.httpContext.current.response.writefile (DiskFileName); System.Web.httpContext.current.Response.end ();

9. The final thing to modify is the ForumContext class. To add an attribute to this class, use the GUID value passed through querystring, find the ForumContext class under the Components project,

First add a variable:

String

Checkguid

=

""

;

The bottom increase in the constructor:

Checkguid

=

Context.Request.QueryString [

"

GUID

"

];

Increase attributes at the bottom:

// Checkguid judges whether or not the legal display file request

Public string checkguid {get {returnch checkguid;}}

At this point, our modification has been completed. The following is to upgrade the original database. Save the data in the Content field to the hard drive. The following code is relatively simple, directly posted the code directly

Add a page under the web project, the operation code of the background is as follows, the code is relatively simple, and it is directly operating the database. Don't forget to increase the reference to PhotoProperties, use

System; using

System.collections; using

System.componentmodel; Using

System.data; using

System.data.sqlclient; using

System.drawing; using

System.Web; Using

System.Web.SessionState; Using

System.web.ui; using

System.Web.ui.WebControls; using

System.Web.ui.htmlControls; Using

System.io; using

JSG.PhotopropertiesLibrary; Namespace

Aspnetforums.Update

{/ ** ////

/// Update_20040807 Abstract Description. /// public class update_20040807: System.Web.UI.Page {protected System.Web.UI.WebControls.Button Button1; private void Page_Load (object sender, System.EventArgs e) {// is placed here User Code In Initialization Page} Web Form Designer Generated Code #Region Web Form Designer Generated Code Override Protected Void OnNit (Eventargs E) {// // Codegen: This call is an ASP.NET Web Form Designer It is necessary. // InitializeComponent (); base.oninit (e);} / ** //// /// Designer supports the required method - do not use the code editor to modify the // / this method.

/// private void InitializeComponent () {this.Button1.Click = new System.EventHandler (this.Button1_Click); this.Load = new System.EventHandler (this.Page_Load);} #endregion private void Button1_Click (object sender, System.EventArgs e) {string ConnectionString = "database = forums; server = dbserver; User ID = sa; Password =;"; SqlConnection myConn = new SqlConnection (ConnectionString); myConn.Open (); string select = "select * from forums_PostAttachments"; SqlCommand myComm = new SqlCommand (select, myConn); SqlDataReader dr = myComm.ExecuteReader (); string DiskFileName, CheckGuid, Exif; string updateString = "Update forums_PostAttachments SET DiskFileName = '{0}', EXIF = '{1}', checkguid = '{2}' where postid = {3} "; string userid; Byte [] FileContent; FileStream Fs = NULL; SqlConnection myconn2 = new sqlconnection; myconn2.open (); while (Dr.Read ()) {userid = DR ["userid"]. Tostring (); filecontent = (byte []) DR ["" ; DiskFileName = Server.MapPath (.. "~ /" AspNetForums.Configuration.ForumConfiguration.GetConfig () UploadFilesPath "/" UserID "/" System.Guid.NewGuid () ToString ()) System.IO .Path.getextension (DR ["filename"]. TOSTRING ()); checkguid =

System.guid.newguid (). Tostring (); fs = new filestream (DiskFileName, FileMode.create, FileAccess.write, Fileshare.Readwrite); fs.write (FileContent, 0, FileContent.Length); fs.close () ; Exif = GetExif (DiskFileName); SqlCommand myComm2 = new SqlCommand (string.Format (updateString, System.IO.Path.GetFileName (DiskFileName), Exif, CheckGuid, dr [ "PostID"] ToString ()), myConn2.); myComm2.ExecuteNonQuery ();} dr.Close ();} private string GetExif (string diskFileName) {string imgFilePath = diskFileName; PhotoProperties pp = new PhotoProperties (); string exif = ""; try {pp.Initialize (); pp .Analyze (imgfilepath); // Exif Version Exif = "EXIF version:" pp.gettagdatum (36864) .prettyprintValue; // Manufacturer EXIF = "Manufacturer:" PP.GettagDatum (271) .prettyprintValue; // Model Exif = "Model:" PP.getTagDatum (272) .prettyprintValue; // Shooting time EXIF ​​ = "shooting time:" Pp.gettagdatum (36868) .PRETTYPRINTVALUE; / / Stream: EXIF ​​ = "
Expressive Time: PP.getTagDatum (33434) .prettyprintValue " Second "; // Aperture Exif =" Aperture: " Pp.gettagdatum (33437) .prettyprintValue; // ISO EXIF ​​ = "ISO:" pp.gettagdatum (34855) .prettyprintValue;

// Resolution EXIF ​​ = "resolution:" pp.gettagdatum (282) .prettyprintValue "/" pp.gettagdatum (283) .prettyprintValue;} catch {} returnif;}}} don't forget the last step Work, delete the Content field in the Forums_PostatTachments table, because we no longer need him.

Finish!

转载请注明原文地址:https://www.9cbs.com/read-121996.html

New Post(0)