asp.net under the fckeditor configuration picture upload the simplest way _ Web editor

Source: Internet
Author: User
Tags httpcontext
1. The original configuration
Delete the fckeditor/filemanager/connectors directory;
Some students may ask, have deleted how to upload files?
Oh...

2. Do not cite FredCK.FCKeditorV2.dll;
Because I was written in JS, do not use the way of control;
In fact, a lot of people on the Internet are trying to inject into the fckeditor/filemanager/connectors directory,
It is true that some people accidentally directly to the FCK editor without any configuration of the situation to the Internet caused by the recruit;

3. Let's get to the point, in general, we don't use FCK, in a system there may be a few places
Now if I had a simple system with "news" releases and, like "Company Profile" one page release two places to use FCK
I'm going to put the pictures of all the articles on/article/yyyymmdd/guid.jpg.
Put all the single pages of the picture on the/page/yyyymmdd/guid.jpg
Or I want to save the picture to a different place by the user ...

4. To achieve
How simple things to say clearly need to do so much matting, too troublesome ...!!!

Web page:
Code
Copy Code code as follows:

<form id= "Form1" runat= "Server" >
<div>
<div>
<asp:textbox id= "TextBox1" textmode= "MultiLine" runat= "Server" > </asp:TextBox>
</div>
<br/>
<asp:button id= "Button1" runat= "Server" text= "button" onclick= "Button1_Click"/>
</div>
</form>


Js:


Code
Copy Code code as follows:

<script src= "Fckeditor/fckeditor.js" type= "Text/javascript" ></script>
<script type= "Text/javascript" >
Window.onload = function () {
var ofckeditor = new FCKeditor (' <%= textbox1.clientid%> ');
Ofckeditor.basepath = ' fckeditor/'
OFCKeditor.Config.ImageUploadURL = "/admin/auploader.aspx";
Ofckeditor.replacetextarea ();
}
</script>


It's done!
Do not believe Ah, the following upload page of the code simply write a bar, this is also more important;

Code
Copy Code code as follows:

protected void Page_Load (object sender, EventArgs e)
{
if (Request.Files.Count > 0)
{
Httppostedfile file = Request.files[0];
String path = "/article/" + System.DateTime.Now.ToString ("yyyyMMdd") + "/";
String serverpath = Server.MapPath (path);
String fileName = Guid.NewGuid () + file. Filename.substring (file. Filename.lastindexof ("."));
if (! System.IO.Directory.Exists (Serverpath))
System.IO.Directory.CreateDirectory (Serverpath);
File. SaveAs (Serverpath + fileName);
Sendfileuploadresponse (0, path + filename, filename, "Upload success!") ");
}
Else
{
Sendfileuploadresponse (1, "", "", "Upload failed! ");
}
}

public void Sendfileuploadresponse (int issucceed, string fileUrl, String fileName, String custommsg)
{
System.Web.HttpContext.Current.Response.Clear ();
System.Web.HttpContext.Current.Response.Write ("<script type= ' Text/javascript ' >");
System.Web.HttpContext.Current.Response.Write (@) (function () {var d=document.domain;while (true) {Try{var a= Window.top.opener.document.domain;break;} catch (e) {};d =d.replace (/.*: \.| $)/, '); if (d.length==0) Break;try{document.domain=d;} catch (e) {break;}}}) ();");
System.Web.HttpContext.Current.Response.Write ("window.parent.OnUploadCompleted" + issucceed.tostring (). ToLower () + ", '" + FileUrl + "', '" + FileName + "', '" + custommsg + "");
System.Web.HttpContext.Current.Response.Write ("</script>");
System.Web.HttpContext.Current.Response.End ();
}


All right, it's done!
This upload code does not do strict suffix limit, size limit, because this page path is our own configuration, we want to write how to write it;
Even you can save to different places depending on the user;
Here need to explain the Sendfileuploadresponse method, in fact, is to send back the message for FCK;
Over here:
System.Web.HttpContext.Current.Response.Write ("window.parent.OnUploadCompleted" + issucceed.tostring (). ToLower () + ", '" + FileUrl + "', '" + FileName + "', '" + custommsg + "");

Corresponds to a
Fckeditor/dialog/fck_image/fck_image.js

Code
Copy Code code as follows:

function onuploadcompleted (errornumber, FileUrl, FileName, custommsg)
{
Remove Animation
Window.parent.Throbber.Hide ();
Gete (' Divupload '). style.display = ';
Switch (errornumber)
{
Case 0://No errors
Alert (' Your file has been successfully uploaded ');
break;
Case 1://Custom Error
alert (custommsg);
return;
Case://Custom Warning
alert (custommsg);
break;
Case 201:
Alert (' A file with the same name is already available. The uploaded file has been renamed to "' + FileName + '");
break;
Case 202:
Alert (' Invalid file type ');
return;
Case 203:
Alert ("Security error.") You are probably don ' t have enough permissions to upload. Please check your server. ") ;
return;
Case 500:
Alert (' The connector is disabled ');
break;
Default:
Alert (' Error on file upload. Error number: ' + errornumber ';
return;
}
Sactualbrowser = ';
SetUrl (FILEURL);
Gete (' Frmupload '). Reset ();
}

Note that 0, is success, 1 is a failure, others to see it ....
By the way here is the 2.65 version of the test, if there are other versions of the flexibility of the same ha;

To sum up:
1. Configure different image processing paths on different pages, such as:
OFCKeditor.Config.ImageUploadURL = "/admin/aupload.aspx";
OFCKeditor.Config.ImageUploadURL = "/admin/bupload.aspx";
。。。。

2. FCk will post the picture to the specified path,
Then you manipulate the picture, according to the directory to save AH or by the current user session to save casually;
Of course, do not forget to verify the identity of the user first, do not have the right to his politeness, directly return the error,
Verify identity sample code is not written inside, you add yourself according to the project

3. Be sure to return the value to tell FCK your operation results, or you will always die there ...

Ps:
It seems that I am not a person who likes to write things, feel very troublesome ...
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.