C # file stream upload picture

Source: Internet
Author: User

1. Define the parameters in the first step

Main Table TableA

public class TableA

{

Public list<tableb> Accidentimgbyte {get; set;}

}

Child table TableB

public class tableb{

Public byte[] Accidentimgflow {get; set;} File stream

Public String Accidentimgname {get; set;} File name

Public String Accidentimgext {get; set;} File format

}

2. Interface

public void Accidentimg_request (TableA tModel) {

String visitimgfile = String. empty;//Picture Path

if (bmmodel.accidentimgbyte! = null)
{
foreach (var item in Tmodel.accidentimgbyte)
{
Visitimgfile + = Attachmenturl (item. Accidentimgflow, item. Accidentimgname, item. Accidentimgext) + ",";

}
if (! String.IsNullOrEmpty (Visitimgfile))
{
Visitimgfile = visitimgfile.substring (0, visitimgfile.length-1);
}
}

}

<summary>
Image processing Methods
</summary>
<param name= "filenameattachment" > File Flow </param>
<param name= "FileName" > File name </param>
<param name= "Fileext" > File format </param>
<returns></returns>

Private String Attachmenturl (byte[] filenameattachment, String fileName, String fileext)
{
String Aurl = system.web.configuration.webconfigurationmanager.appsettings["web. config config upload address"];//server address, Used to return the server picture address combination, such as: http://x.x.x/
Try
{
Write directory
String AttachmentPath = system.web.configuration.webconfigurationmanager.appsettings["web. config config upload address"] + fileName + Fileext;
File.writeallbytes (AttachmentPath, filenameattachment);
Aurl = Aurl + fileName + fileext;//stitching return address such as: http://x.x.x/test 1.jpg
}
catch (Exception ex)
{
HX.BPM.Common.LogWriter.Write ("Log", "Baggagemonitoradd_request", "Baggage monitoring process Initiation handling Exception", ex. Message);
Aurl = null;
}

return aurl;
}

3. Front-end (external) call

private void Button1_Click (object sender, EventArgs e)
{

TableA ta=new TableA ();

Ta. accidentimgbyte = new attmodel[2];//definition length

byte[] Aimag = filecontent (@ "Local picture path");

Ta. Accidentimgbyte[0]=new TableB () {
Accidentimgflow = Aimag,
Accidentimgname = "Test 1",
accidentimgext= ". jpg"
};

Aimag = filecontent (@ "Local picture path");
Ta. ACCIDENTIMGBYTE[1] = new TableB ()
{
Accidentimgflow = Aimag,
Accidentimgname = "Test 2",
Accidentimgext = ". jpg"
};

var result = Accidentimg_request (TA);

}

Convert files into streams

private static byte[] Filecontent (string fileName)
{
FileStream fs = new FileStream (FileName, FileMode.Open, FileAccess.Read);
Try
{
byte[] Buffur = new Byte[fs. Length];
Fs. Read (Buffur, 0, (int) fs. Length);

return Buffur;
}
catch (Exception ex)
{
return null;
}
Finally
{
if (fs! = NULL)
Fs. Close ();
}
}

C # file stream upload picture

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.