C # Implementation of image upload (PC end and app) save and cross-domain upload instructions _c# tutorial

Source: Internet
Author: User
Tags bool create directory datetime httpcontext

A-PC End:

1-page--multiple is the control of a single or more than one picture upload

<input id= "Busroute" type= "file" class= "btn btn-default btn-lg" style= "Height:34px;padding-top:5px;padding-bottom : 5px; "Multiple/>

2-Background to get picture files:

Httpfilecollection pcfilecoll = HttpContext.Current.Request.Files;

3-save Example:

 #region Create a directory//full storage path string completeurl = "";
Relative rank path string relativeurl = "";
String Savetemppath = "~/resources/pic";
String picuploadpath = HttpContext.Current.Server.MapPath (Savetemppath);
Add root directory Completeurl = @ "\\10.0.8.52\YuanXinFiles\Office\";;
Add a level directory string relativeoneurl = DateTime.Now.Year.ToString ();
Completeurl + = "\" + Relativeoneurl;
Relativeurl + = "\" + Relativeoneurl; if (! Directory.Exists (Completeurl)) {directory.createdirectory (completeurl);}//Add level two directory string relativetwourl =
DateTime.Now.Month.ToString ();
Completeurl + = "\" + Relativetwourl;
Relativeurl + = "\" + Relativetwourl; if (! Directory.Exists (Completeurl)) {directory.createdirectory (completeurl);} #endregion//Save Httpfilecollection PicColl
= Picmodel.pcfilecoll; for (var i = 0; i < Piccoll.count i++) {httppostedfile file = Piccoll[i];//Save picture//save to specified directory file.
SaveAs (Completeurl + "\" + fileName); }

B-app:

What the front page looks like regardless of, the backstage gets is the Base64 string collection.

1-save Example:

#region Create directory//full storage path string completeurl = "";
Relative rank path string relativeurl = "";
String Savetemppath = "~/resources/pic";
String picuploadpath = HttpContext.Current.Server.MapPath (Savetemppath);
Add root directory Completeurl = @ "\\10.0.8.52\YuanXinFiles\Office\";;
Add a level directory string relativeoneurl = DateTime.Now.Year.ToString ();
Completeurl + = "\" + Relativeoneurl;
Relativeurl + = "\" + Relativeoneurl; if (! Directory.Exists (Completeurl)) {directory.createdirectory (completeurl);}//Add level two directory string relativetwourl =
DateTime.Now.Month.ToString ();
Completeurl + = "\" + Relativetwourl;
Relativeurl + = "\" + Relativetwourl; if (! Directory.Exists (Completeurl)) {directory.createdirectory (completeurl);} #endregion//save byte[] bytes =
Convert.frombase64string (Strpic.piccode);
MemoryStream memstream = new MemoryStream (bytes);
BinaryFormatter Binformatter = new BinaryFormatter ();
System.Drawing.Bitmap map = new Bitmap (memstream); 
Image image = (image) map; String imagename = Guid.NewGuid (). ToString ("N "); Save image Image. Save (Completeurl + "\" + imagename + "." + Strpic.pictype); Save picture

C-cross-domain Save problem:

Common scenarios across domains are shown in the following illustration: We use the computer's network to insinuate, connect to the desired directory, where you can add a person account with permissions to access the target folder, so how do you get access using C # code?

To get the above access rights, you need to reference a class and add some simple code:

1-Access Code:

<summary>
///The last picture operation by specifying
the user///</summary>
///<param name= "Uploadaction" ></ param> public
void Uploadfilebyuser (Action uploadaction)
{
//reference class: D:\SourceCode\MCSFramework\02. Develop\mobilewebapp\yuanxin\services\fileuploadservice\controllers\uploadcontroller.cs
// Unable to authenticate with permission-only
try
{
var ip = "10.0.8.52"
can be accessed through the extranet; var domain = "Sinooceanland";
var username = configurationmanager.appsettings["Uploadusername"]. ToString (); Configured username
var pwd = configurationmanager.appsettings["Uploadpassword"]. ToString (); The configured password
var root = configurationmanager.appsettings["Uploadrootpath"]. ToString (); The configured file root path
using (networkshareaccesser.access (IP, domain, username, pwd))//establishes the connection
{
uploadaction ();// Picture Save Code
}
catch (System.Exception e)
{
}
}

2-Must class:

public class Networkshareaccesser:idisposable {private string _remoteuncname; private string _remotecomputername; publ IC string RemoteComputerName {get {this._remotecomputername;} set {this._remotecomputername = value; this._remo
Teuncname = @ "\" + this._remotecomputername; UserName {get; set;} public string Password {get; set;} #region consts Private const int RESOURCE_CO
nnected = 0x00000001;
Private Const int resource_globalnet = 0x00000002;
Private Const int resource_remembered = 0x00000003;
Private Const int resourcetype_any = 0x00000000;
Private Const int resourcetype_disk = 0x00000001;
Private Const int resourcetype_print = 0x00000002;
Private Const int resourcedisplaytype_generic = 0x00000000;
Private Const int resourcedisplaytype_domain = 0x00000001;
Private Const int resourcedisplaytype_server = 0x00000002;
Private Const int resourcedisplaytype_share = 0x00000003;
Private Const int resourcedisplaytype_file = 0x00000004; Private Const int RESourcedisplaytype_group = 0x00000005;
Private Const int resourceusage_connectable = 0x00000001;
Private Const int resourceusage_container = 0x00000002;
Private Const int connect_interactive = 0x00000008;
Private Const int connect_prompt = 0x00000010;
Private Const int connect_redirect = 0x00000080;
Private Const int connect_update_profile = 0x00000001;
Private Const int connect_commandline = 0x00000800;
Private Const int connect_cmd_savecred = 0x00001000;
Private Const int connect_localdrive = 0x00000100;
#endregion #region Errors Private Const int no_error = 0;
Private Const int error_access_denied = 5;
Private Const int error_already_assigned = 85;
Private Const int error_bad_device = 1200;
Private Const int error_bad_net_name = 67;
Private Const int error_bad_provider = 1204;
Private Const int error_cancelled = 1223;
Private Const int error_extended_error = 1208;
Private Const int error_invalid_address = 487;
Private Const int error_invalid_parameter = 87; Private Const int ERROR_INValid_password = 1216;
Private Const int ERROR_MORE_DATA = 234;
Private Const int ERROR_NO_MORE_ITEMS = 259;
Private Const int error_no_net_or_bad_path = 1203;
Private Const int error_no_network = 1222;
Private Const int error_bad_profile = 1206;
Private Const int error_cannot_open_profile = 1205;
Private Const int error_device_in_use = 2404;
Private Const int error_not_connected = 2250;
Private Const int error_open_files = 2401; #endregion #region PInvoke signatures [DllImport ("Mpr.dll")] private static extern int wnetuseconnection (INTPTR Hwndowne R, Netresource Lpnetresource, String Lppassword, string lpuserid, int dwflags, string lpaccessname, string lpbuffersize, S
Tring Lpresult);
[DllImport ("Mpr.dll")] private static extern int WNetCancelConnection2 (string lpname, int dwflags, bool fforce); [StructLayout (layoutkind.sequential)] private class Netresource {public int dwscope = 0; public int dwtype = 0;
T dwdisplaytype = 0;
public int dwusage = 0; public string Lplocalname = "";
public string lpremotename = "";
public string lpcomment = "";
public string lpprovider = "";} #endregion///<summary>///creates a networkshareaccesser for the given computer name. The user is promted to enter credentials///</summary>///<param name= "RemoteComputerName" &GT;&LT;/PARAM&G
T <returns></returns> public static Networkshareaccesser Access (string remotecomputername) {return new
Networkshareaccesser (RemoteComputerName);  }///<summary>///creates a networkshareaccesser for the given computer name using the given domain/computer name, Username and password///</summary>///<param name= "RemoteComputerName" ></param>///<param name= "Domainorcomutername" ></param>///<param name= "UserName" ></param>///<param name= "Password" "></param> public static Networkshareaccesser Access (String remotecomputername, String domainorcomutername, String userName, String password) {RETUrn New Networkshareaccesser (remotecomputername, domainorcomutername + @ "\" + userName, password); }///<summary>///creates a networkshareaccesser for the given computer name using the given username (format:dom ainorcomputername\username) and password///</summary>///<param name= "RemoteComputerName" ></param >///<param name= "UserName" ></param>///<param name= "password" ></param> public static Networkshareaccesser Access (String remotecomputername, String userName, string password) {return new
Networkshareaccesser (remotecomputername, userName, password); Private Networkshareaccesser (String remotecomputername) {remotecomputername = RemoteComputerName;
Connecttoshare (this._remoteuncname, NULL, NULL, TRUE); Private Networkshareaccesser (String remotecomputername, String userName, string password) {remotecomputername = remote
ComputerName;
UserName = UserName;
Password = Password; This. Connecttoshare (This._remoteuncname, this. USername, this.
Password, false); private void Connecttoshare (String remoteunc, string Username, string password, bool promptuser) {Netresource nr = new
Netresource {dwtype = resourcetype_disk, lpremotename = Remoteunc};
int result; if (promptuser) {result = WNetUseConnection (IntPtr.Zero, nr, "", "", Connect_interactive |
Connect_prompt, NULL, NULL, NULL); 
else {result = WNetUseConnection (IntPtr.Zero, nr, password, username, 0, NULL, NULL, NULL);} if (Result!= no_error) {
throw new Win32Exception (result); }} private void Disconnectfromshare (string remoteunc) {int result = WNetCancelConnection2 (Remoteunc, Connect_update_pro
FILE, false);
If (result!= no_error) {throw new win32exception (result);} <summary>///performs application-defined tasks associated with freeing, releasing, or resetting unmanaged Reso
Urces. </summary>///<filterpriority>2</filterpriority> public void Dispose () {this.
Disconnectfromshare (This._remoteuncname); }
}

The above is a small part of the introduction of C # to realize the image upload (PC end and app) save and cross-domain upload instructions, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.