Upload pictures and files with Ueditor and wait until seven kn cloud storage

Source: Internet
Author: User
Tags file url

Ueditor uploads files, which are uploaded in the form of data streams.

The seven Qiniu store official documents only provide a way to upload file paths.

But this is only the way it was written in official documents.

In fact, using the object manager of VS to open the Qiniu DLL, we can see the following things:

In fact, Qiniu provides the SDK, you can use the file stream to upload files.

So, according to the examples provided in the official documentation, we can rewrite the upload to look like this:

    /// <summary>    ///Uploading Files/// </summary>    /// <param name= "key" >the saved file name</param>    /// <param name= "FileStream" >Local file path</param>     Public Static voidPutfile (stringkey, Stream FileStream) {        varPolicy =NewPutpolicy (Bucket,3600); stringUptoken =policy.        Token (); PutExtra Extra=NewPutExtra (); Ioclient Client=Newioclient (); Client.    Put (Uptoken, Key, FileStream, extra); }

In fact, only modified the parameters of the putfile, I will bucket (seven of the space on the cow) made a member variable.

Another change is to use stream instead of the original file name.

The last client call is also used to view the put method above.

After writing the upload method, we open the editor's UploadHandler.cs file, modify some of the contents, and upload the file with the file stream:

        //var savepath = Pathformatter.format (Uploadfilename, Uploadconfig.pathformat); //var localPath = Server.MapPath (Savepath);        Try        {            //if (! Directory.Exists (Path.getdirectoryname (LocalPath)))//{            //directory.createdirectory (Path.getdirectoryname (LocalPath)); //}            //file.writeallbytes (LocalPath, uploadfilebytes);Qiniu.Conf.Config.ACCESS_KEY ="****************************************"; Qiniu.Conf.Config.SECRET_KEY="****************************************"; Putfile (Uploadfilename,NewMemoryStream (uploadfilebytes)); Result.url=Makegettoken (uploadfilename); Result.state=uploadstate.success; }

The comment section is the case code provided by Ueditor, and we create a MemoryStream object that is passed to the Putfile stream parameter.

You cannot use file directly here. InputStream.

The last Makegettoken method, used to generate the uploaded file path, is also modified from the official document.

Because I'm using a private space of seven qiniu, I need to take this step more:

    /// <summary>    ///get the file URL path after uploading. /// </summary>    /// <param name= "key" >file name</param>    /// <returns>URL path with download password</returns>     Public Static stringMakegettoken (stringkey) {        stringBASEURL =getpolicy.makebaseurl (domain, key); stringPrivate_url =getpolicy.makerequest (BASEURL); returnPrivate_url; }

Again, I'll set domain as a member variable.

Finally, be sure to remember the ueditor in the Config.json "Imageurlprefix" value is set to empty, or you upload successfully, in the Ueditor is also unable to directly view the uploaded content, Because will Imageurlprefix will default to load your file link in front.

Upload pictures and files with Ueditor and wait until seven kn cloud storage

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.