Use ueditor to upload images and files to qiniu cloud storage.

Source: Internet
Author: User

Use ueditor to upload images and files to qiniu cloud storage.

Ueditor uploads files in the form of data streams.

The official document of qiniu cloud storage only provides the file path upload method.

However, this method is only written in official documents.

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

In fact, in the SDK provided by Qiniu, files can be uploaded using file streams.

Therefore, based on the cases provided in the official document, we can rewrite the upload as follows:

/// <Summary> /// upload a file /// </summary> /// <param name = "key"> saved file name </param> /// <param name = "fileStream"> local file path </param> public static void PutFile (string key, stream fileStream) {var policy = new PutPolicy (bucket, 3600); string upToken = policy. token (); PutExtra extra = new PutExtra (); IOClient client = new IOClient (); client. put (upToken, key, fileStream, extra );}

In fact, only the PutFile parameter is modified. I made the bucket (the space name on qiniu) into a member variable.

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

The final client call also uses the Put method shown above.

After writing the upload method, open the editor's UploadHandler. cs file, modify some of the content, and upload the file using 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, new MemoryStream(uploadFileBytes));            Result.Url = MakeGetToken(uploadFileName);            Result.State = UploadState.Success;        }

The annotated part is the case code provided by ueditor. We create a MemoryStream object and pass it to the Stream parameter of PutFile.

File. InputStream cannot be used directly.

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

Because I use the private space of qiniu cloud, I need to perform the following steps:

/// <Summary> /// obtain the URL of the uploaded file. /// </Summary> /// <param name = "key"> file name </param> /// <returns> URL path with the download password </returns> public static string MakeGetToken (string key) {string baseUrl = GetPolicy. makeBaseUrl (domain, key); string private_url = GetPolicy. makeRequest (baseUrl); return private_url ;}

Similarly, here I set domain as a member variable.

Finally, remember to set config in ueditor. the value of json "imageUrlPrefix" is set to null. Otherwise, the uploaded content cannot be directly viewed in ueditor after the upload is successful, because imageUrlPrefix will load your file link by default.

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.