Display a component of Binary (2) in the web page (2)

xiaoxiao2021-03-06  123

First, write in front:

Because the work is more tight! The code will write more mind, so the programmer must pay attention to rest, don't stay up late, the saying goes well: "If anything can be, you can't have disease!";

Last night, I experienced VS2005, really different, I can't admire M $ vision and imagination. Farth affecting every programmer. Whether it is from the interface experience, functional consideration, humanity, code, and the simpleness of the code, he brings us that the efficiency improvement, and simplification of things. Simple is the best.

We don't need to work overtime, maybe it will not be too far ...

Let us come together!

Second, the principle:

All right! Sometimes we always like to speak nonsense. But sometimes nonsense is very adjustable, then continue:

ASP.NET displays the only way to click on the bitmap (because I only know such a method ^ _ ^) is on a page, and then in that leaves, it will take the bitmap Respone to the leaves! But it is a trouble. That's it:

SRC = "Region.BinaryImage / FCCC37AB-F4CB-4199-8332-7FF0168DaAd4.aspx".

Then you have to integrate such a process into the formation, you can waste time without a goal of building a target leaf, so that you can save anything (because I am more lazy, so I prefer to write some components to meet my inertia ).

After careful research (^_^), a solution:

Components automatically generate a related directory, built a configuration file in this directory to locate the response to our custom type.

Third, component technology:

So in order to achieve this great component (^ _ ^), we need some components technology.

1. The most important thing is that the leaves are positioned, in the configuration file node

Relocate to our custom type.

2, how to implement a custom type. Provide an interface in the .NET Framework to achieve this effect, our custom type region.controls.binaryImageHandler must inherit your two interfaces from System.Web.ihttphandler. (Due to the relationship between the space, the more in-depth principle is not here, if you are interested, you can view MSDN.

3. Is there anything about how the bitmap is passed from the source page to the target page? Since we don't have a target page, all handles are handled in our custom type, and the objects such as the HTTPHANDLER have been sent yet, the only feasible method is that the bitmap is saved in the application. You can delete it after you run.

4, then there are several places to pay attention to the ID of the bitmap in Application because it is global. So you can use a GUID.

Basically, you can so much!

The source code is sent to the bottom, the new class library project will be COPY below and then compile! :( Because I have no space, I have no space to provide download, if a good friend provides me to upload it to everyone to download) #region [binaryImage show coponent]

/// author: region

///Msn: hl_net@msn.com

///Blog: http://blog.9cbs.net/xinyulou

#ndregion

Using system;

Using system.io;

Using system.Web;

Using System.Resources;

Using system.reflection;

Namespace Region.Controls

{

///

/// Display bitmap

///

///

/// <% @ register tagprefix = "region" namespace = "region.controls" assembly = "region.controls"%>

///

///

//[System.web.ui.toolboxdata("< 2}: binaryimage runat = server> ")]

Public class binaryImage: system.web.ui.webcontrols.image: PUBLIC CLASS BINARYIMAGE:

{

String path1;

Protected Override Void OnLoad (Eventargs E)

{

Base.onLoad (e);

Path1 = path.combine (page.Request.PhysicalApplicationPath, path.getFileNamewithOUTEXTENSITION (Assembly.GetexecutingAnsembly (). location));

String path2 = path.combine (pat1, "web.config");

IF (! Directory.exists (Path1))

{

Directory.createdIRectory (Path1);

}

IF (! file.exists (pat2))

{

// ResourceManager RM = New ResourceManager (asmbly.GetexecutingAnsembly (). Location) ". String", assembly.getexecutingassembly ());

// String a = rm.getstring ("config");

String a = "" @ "

" @"

" @"

" @" " " " @"

" @"

" @"

" @"

"

BYTE [] BS = system.text.Encoding.default.getbytes (a);

FileStream FileStream = New FileStream (Path2, FileMode.create, FileAccess.write);

Try

{

FileStream.write (BS, 0, BS.LENGTH);

}

Finally

{

IF (fileStream! = null)

{

(IDisposable) filestream .dispose ();

}

}

}

}

// Unique ID

Public String GUID

{

get

{

if (ViewState [UniqueID] == NULL)

ViewState [UniqueID] = guid.newguid (). TOSTRING ();

Return (String) ViewState [UniqueID];

}

}

//[System.componentmodel.browsable (face)]]

Public Byte [] binary

{

Set {page.Session [guid] = value;

Get {return page.Session [guid] == null? null: (byte []) Page.Session [guid];

}

Protected Override Void Render (System.Web.ui.htmlTextWriter Writer)

{

IF (imageurl == "")

{

ImageURL = path.combine (path.getfilenamewithoutextension (asmbly.getexecutingassembly (). Location, guid ". Aspx");

IF (Page.Application [GUID] == NULL)

= Binary;

}

Base.render (Writer);

}

}

Public class binaryImageHandler: System.Web.ihttphandler

{

Public Virtual Bool IsReusable

{

Get {return true;}

}

Public Virtual Void ProcessRequest (HTTPContext Context)

{

String id = path.getFileNamewithoutextension (Context.Request.FilePath);

IF (Context.Application [ID]! = null) {

BYTE [] BS = (byte []) context.application [id];

HttpContext.current.response.binarywrite (BS);

Context.Application [ID] = NULL;

}

}

}

Fourth, write in the last:

Listen to melodious music, and experience the beauty of Codes' poems.

that is it

Share U Opinion

If you have any thoughts, please write down!

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

New Post(0)