[WebApi] Tinkering with a resource Manager--File download

Source: Internet
Author: User
Tags browser cache

Creating a file Management interface for a website or other Web application (WEBAPI) Chapter One----"File Download"

========================================================
Qiujuer
Blog: Blog.csdn.net/qiujuer
Website: www.qiujuer.net
Open Source Library: genius-android
Reprint Please specify source: blog.csdn.net/qiujuer/article/details/41621781
========================================================

In this

To access an online picture resource, I think you should have encountered such a situation:

Under normal circumstances:http://www.qiujuer.net/Statics/Image/Home/HeadPortrait.png

And, of course, this:Http://www.qiujuer.net/Resource/75CDF243C30750D397A90E58D412B22E



You can see that you get the same picture but you can take a different address, especially in the second address without a file suffix.

This is a wonderful flower, isn't it? Does it feel weird?

Actually, it's simple.

In the first case , the address of the current picture that is accessed is stored in the site server.

In other words, the address stored in the server on the "cdn.duitang.com" Web site should be "uploads/item/201403/04 " under the folder " 20140304122431_ Xmcuj.jpeg"file.

When you enter the address in the first image, the Web server will search for the file in its designated folder and return to the image.

In the second case , you can say "www.qiujuer.net" in the "Resource" folder. 75cdf243c30750d397a90e58d412b22e"file?

There is such a possibility, but not in this case!

It works by accessing the current URL, triggering the "Resource" interface, and then passing in the id= "75cdf243c30750d397a90e58d412b22e".

Then webapi the server according to the ID through a certain way to find the image resources, and then through the way of resources to return pictures;

And the picture is exactly where you don't know, it could be a folder in the current web directory, or it could be another disk; In short, it's not fixed.


Can you use crawler software to crawl the image for the second case? If so, but I will add the specified user rights to this interface?

Say so much, we come to realize it!

Codetime

First open vs-New project-web application-Name "WebResource"-webapi


Go to Project-delete Valuescontroller

Add a WEBAPI controller-Resourceapicontroller


In which the Get method is implemented:

    [Routeprefix ("Resource")]    public class resourceapicontroller:apicontroller    {& nbsp       private static readonly long memory_size = * 1024x768 * 1024;        private Stati c readonly String root_path = HttpContext.Current.Server.MapPath ("~/app_data/");        [httpget]& nbsp       [Route ("{Id}")]        Public async taskBasically all are commented, if have not understood also please write down in the comment.

Runtime prep Work

We need to create the folder "Image" under "App_Data" before running. Then copy two images "001.png" \ "002.png" into it.

Run

Click Run-Enter the URL in the browser:http://localhost:60586/Resource/001

Replace the ID with 002 re-enter:http://localhost:60586/Resource/002


OK, the basic function we have achieved.

Why is END time a "App_Data" folder?

Here it is necessary to say that "App_Data" is a protected object in the. NET Web. cannot be accessed directly via URL;

To try, enter the URL:http://localhost:60586/App_Data/Image/002.png


So, the general database file can be placed under the folder, of course, now you can put the picture below, and then access through the interface.

Is it too simple?

Indeed, the first chapter here is just the beginning, and the subsequent enlargement is gradual.

For example, add: Upload, other non-picture files, and not 001,002 name, but the operation of an ID out, of course, plus data management and so on.

Code

This time the code is packed, but csdn can't get up, I go! But the good is simple; just one way; package it together in the next chapter.

[WebApi] Tinkering with a resource Manager--File download

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.