Web Api upload image, resolve upload picture unformatted

Source: Internet
Author: User
Tags allkeys

When making this function, found a lot of information, but forget the address, so do not put together a connection,

Go directly to the code!

1. First create a new controller to upload

    

<summary>

Upload

</summary>

[Routeprefix ("Api/upload")]

public class Uploadcontroller:apicontroller

2. There is a controller, must have a place to accept

<summary>
Upload image
</summary>
<returns></returns>
<exception cref= "System.Web.Http.HttpResponseException" ></exception>
[HttpPost, Route ("Image")]
Public async task

3. Important code, expand (rewrite) the Multipartformdatastreamprovider class yourself

We implement Multipartformdatastreamprovider to override the filename of File which

would be stored on server, or else the default name would be is of the the format like body-

Part_{guid}. In the following implementation we simply get the FileName from

Contentdisposition Header of the Request Body.

public class Custommultipartformdatastreamprovider:multipartformdatastreamprovider

{

<summary>

User number is prefixed by production user picture

<para> If the user is 01,01-12345467878</para>

</summary>

public string UserId {get; set;}

<summary>

Picture name

</summary>

public string ImageName {get; set;}

<summary>

Initializes a new instance of the <see cref= "Custommultipartformdatastreamprovider"/> class.

</summary>

<param name= "userId" >the user identifier.</param>

<param name= "ImageName" ></param>

<param name= "path" >the path.</param>

Public Custommultipartformdatastreamprovider (String userId, String imageName, String path)

: Base (Path)

{

This. UserID = userid;

This. ImageName = ImageName;

}

<summary>

Gets the local file name, which is combined with the root path used to create the absolute file name that stores the contents of the current MIME body part.

</summary>

<param name= "Headers" > the header of the current MIME body part. </param>

<returns>

The relative file name of the path part is not included.

</returns>

public override string Getlocalfilename (Httpcontentheaders headers)

{

Name of the saved picture

var serverimagename = "";

if (!string. IsNullOrEmpty (Serverimagename))

{

Serverimagename = this. ImageName;

}

Else

{

Get file name when new

Serverimagename = headers. ContentDisposition.FileName.Replace ("\" ", String.) Empty);

Use. Split

Take the last picture type

var Splitarr = Serverimagename.split ('. ');

var nowtime = DateTime.Now.ToString ("YyyyMMdd");

Serverimagename = this. UserId + "-" + Nowtime + "." + Splitarr.lastordefault ();

}

return serverimagename;

}

}

4. All the source code in the class is below:

Using system;using system.collections.generic;using system.io;using system.linq;using System.Net;using System.net.http;using system.net.http.headers;using system.threading.tasks;using System.Web;using System.Web.Http; namespace payu_file.controllers{///<summary>///upload//</summary>[routeprefix ("Api/upload")]public Class uploadcontroller:apicontroller{///<summary>///upload images///</summary>///<returns></returns >///<exception cref= "System.Web.Http.HttpResponseException" ></exception>[httppost, Route ("Image") ]public Async task

  

Web Api upload image, resolve upload picture unformatted

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.