欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现图片上传(PC端和APP)保存及 跨域上传说明

程序员文章站 2022-06-22 16:02:28
a-pc端: 1-页面--multiple是控制单张还是多张图片上传

a-pc端:

1-页面--multiple是控制单张还是多张图片上传

<input id="busroute" type="file" class="btn btn-default btn-lg" style="height:34px;padding-top:5px;padding-bottom:5px;" multiple />

2-后台获取图片文件:

httpfilecollection pcfilecoll = httpcontext.current.request.files;

3-保存示例:

#region 创建目录
//完整存储路径
string completeurl = "";
//相对等级路径
string relativeurl = "";
//string savetemppath = "~/resources/pic";
//string picuploadpath = httpcontext.current.server.mappath(savetemppath);
//添加根目录
completeurl = @"\\10.0.8.52\yuanxinfiles\office\"; ;
//添加一级目录
string relativeoneurl = datetime.now.year.tostring();
completeurl += "\\" + relativeoneurl;
relativeurl += "\\" + relativeoneurl;
if (!directory.exists(completeurl))
{
directory.createdirectory(completeurl);
}
//添加二级目录
string relativetwourl = datetime.now.month.tostring();
completeurl += "\\" + relativetwourl;
relativeurl += "\\" + relativetwourl;
if (!directory.exists(completeurl))
{
directory.createdirectory(completeurl);
}
#endregion
//保存
httpfilecollection piccoll = picmodel.pcfilecoll;
for (var i = 0; i < piccoll.count; i++)
{
httppostedfile file = piccoll[i];
//保存图片
//保存至指定目录
file.saveas(completeurl + "\\" + filename);
}

b-app:

前端页面长什么样不管了,后台拿到的是base64的字符串集合.

1-保存示例:

#region 创建目录
//完整存储路径
string completeurl = "";
//相对等级路径
string relativeurl = "";
//string savetemppath = "~/resources/pic";
//string picuploadpath = httpcontext.current.server.mappath(savetemppath);
//添加根目录
completeurl = @"\\10.0.8.52\yuanxinfiles\office\"; ;
//添加一级目录
string relativeoneurl = datetime.now.year.tostring();
completeurl += "\\" + relativeoneurl;
relativeurl += "\\" + relativeoneurl;
if (!directory.exists(completeurl))
{
directory.createdirectory(completeurl);
}
//添加二级目录
string relativetwourl = datetime.now.month.tostring();
completeurl += "\\" + relativetwourl;
relativeurl += "\\" + relativetwourl;
if (!directory.exists(completeurl))
{
directory.createdirectory(completeurl);
}
#endregion 
//保存
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");
//保存图片
image.save(completeurl + "\\" + imagename + "." + strpic.pictype); //保存图片

c-跨域保存问题:

跨域的常见场景如下图所示:我们通过电脑的网络影射,连接到所需要的目录,这里添加上拥有权限的人员账号即可访问目标文件夹,那么使用c#代码如何获得访问权限呢?

C#实现图片上传(PC端和APP)保存及 跨域上传说明

要获取以*问权限,需要引用一个类和添加一些简单代码:

1-访问代码:

/// <summary>
/// 通过指定用户执行上次图片操作
/// </summary>
/// <param name="uploadaction"></param>
public void uploadfilebyuser(action uploadaction)
{
//参考类:d:\sourcecode\mcsframework\02.develop\mobilewebapp\yuanxin\services\fileuploadservice\controllers\uploadcontroller.cs
//无法通过权限认证--只能通过外网访问
try
{
var ip = "10.0.8.52";
var domain = "sinooceanland";
var username = configurationmanager.appsettings["uploadusername"].tostring(); //配置的用户名
var pwd = configurationmanager.appsettings["uploadpassword"].tostring(); //配置的密码
var root = configurationmanager.appsettings["uploadrootpath"].tostring(); //配置的文件根路径
using (networkshareaccesser.access(ip, domain, username, pwd)) //建立连接
{
uploadaction(); //图片保存代码
}
}
catch (system.exception e)
{
}
}

2-必须类:

public class networkshareaccesser : idisposable
{
private string _remoteuncname;
private string _remotecomputername;
public string remotecomputername
{
get
{
return this._remotecomputername;
}
set
{
this._remotecomputername = value;
this._remoteuncname = @"\\" + this._remotecomputername;
}
}
public string username
{
get;
set;
}
public string password
{
get;
set;
}
#region consts
private const int resource_connected = 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 hwndowner,
netresource lpnetresource,
string lppassword,
string lpuserid,
int dwflags,
string lpaccessname,
string lpbuffersize,
string 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;
public int 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 will be promted to enter credentials
/// </summary>
/// <param name="remotecomputername"></param>
/// <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: domainorcomputername\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;
this.connecttoshare(this._remoteuncname, null, null, true);
}
private networkshareaccesser(string remotecomputername, string username, string password)
{
remotecomputername = remotecomputername;
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_profile, false);
if (result != no_error)
{
throw new win32exception(result);
}
}
/// <summary>
/// performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
/// <filterpriority>2</filterpriority>
public void dispose()
{
this.disconnectfromshare(this._remoteuncname);
}
}

以上所述是小编给大家介绍的c#实现图片上传(pc端和app)保存及 跨域上传说明,希望对大家有所帮助