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

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#代码如何获得访问权限呢?

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

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,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,userName,password);
}
private NetworkShareAccesser(string remoteComputerName)
{
RemoteComputerName = remoteComputerName;
this.ConnectToShare(this._remoteUncName,null,true);
}
private NetworkShareAccesser(string remoteComputerName,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);
}
else
{
result = WNetUseConnection(IntPtr.Zero,password,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)保存及 跨域上传说明,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


文章浏览阅读6.2k次,点赞2次,收藏3次。C#数学运算表达式解释器测试文件内容:a=2+3*2;b=2*(2+3);浏览按钮事件处理程序: private void button_browse_Click(object sender, EventArgs e) { OpenFileDialog fbd = new OpenFileDialog(); fbd.T_c# 表达式分析器
文章浏览阅读5.2k次,点赞6次,收藏7次。程序要做到用户配置的灵活性,就需要添加配置管理功能,这里使用.NET的应用程序配置文件app.config来保存配置信息,.NET Framework提供了对配置文件读写的良好支持。要实现配置文件的读取功能,需要引用System.Configuration命名空间。提供源码下载,有源有真相。_引用封送类的字段,访问上面的成员可能导致运行时异常
文章浏览阅读9k次。错误信息检测到 ContextSwitchDeadlock Message: CLR 无法从 COM 上下文 0x622b440 转换为 COM 上下文 0x622b5b0,这种状态已持续 60 秒。拥有目标上下文/单元的线程很有可能执行的是非泵式等待或者在不发送 Windows 消息的情况下处理一个运行时间非常长的操作。这种情况通常会影响到性能,甚至可能导致应用程序不响应或者使用的内存随时间不断_contextswitchdeadlock
文章浏览阅读2w次,点赞10次,收藏9次。我发生错误时的环境:Windows 7,Framework 4、0,Microsoft Office 2007,VS2010,c# WinForm;部分代码: string strConn = "Provider=Microsoft.Ace.OleDb.12.0;Persist Security Info=False;" + "data source=" + _c# oledb 操作必须使用一个可更新的查询
文章浏览阅读9.8k次。C# 二进制字节流查找函数IndexOf /// /// 报告指定的 System.Byte[] 在此实例中的第一个匹配项的索引。 /// /// 被执行查找的 System.Byte[]。 /// 要查找的 System.Byte[]。 /// 如果找到该字节数组,则为 searchBytes 的索_c#byte[]查找
文章浏览阅读2.5w次,点赞3次,收藏9次。c#DataGridView数据绑定示例 格式化单元格的内容在使用DataGridView显示数据库中的数据时,我们需要对某列的数据显示格式进行格式化。这里使用实时构建的数据,如下图:在显示时对第三列的数据进行格式化,如下图:测试数据构建及数据绑定: private void Form1_Load(object sender, EventArgs e) { _c#datatable列格式化
文章浏览阅读2.8w次,点赞3次,收藏4次。完整错误信息错误 1 命名空间“System”中不存在类型或命名空间名称“Linq”。是否缺少程序集引用? F:CsProjectsCSharp实现SPY++CSharp实现SPY++Form1.cs 6 14 CSharp实现SPY++错误原因开始的时候创建项目选择的Framework版本是4.0,但后来为了项目的平台适应性,将Framework的版本改为了2.0,重新编译_命名空间system中不存在类型或命名空间名称
文章浏览阅读1.9w次。一、通过配置文件实现以管理员身份运行程序Vista 和 Windows 7 操作系统为了加强安全,增加了 UAC(用户账户控制) 的机制,如果 UAC 被打开,用户即使是以管理员权限登录,其应用程序默认情况下也无法对系统目录,系统注册表等可能影响系统运行的设置进行写操作。这个机制大大增强了系统的安全性,但对应用程序开发者来说,我们不能强迫用户去关闭UAC,但有时我们开发的应用程序又需要_c# 默认程序以管理身份运行。
文章浏览阅读5.2k次。在使用C#操作IIS创建应用程序池出现异常:无效索引(Exception from HRESULT:0x80070585)相关代码:public static string CreateAppPool(string appPoolName, string frameworkVersion, string managedPipelineMode) {_create website 无效索引。 (0x80070585)
文章浏览阅读9.5k次,点赞3次,收藏4次。C#二进制字节数组操作函数 截取字节数组SubByte /// /// 截取字节数组 /// /// 要截取的字节数组 /// 开始截取位置的索引 /// 要截取的字节长度 /// 截取后的字节数组 public byte[] SubByte(byte[] srcByt_c#字节数组截取
文章浏览阅读2.4w次,点赞5次,收藏16次。C#是微软公司发布的一种面向对象的、运行于.NET Framework之上的高级程序设计语言。并定于在微软职业开发者论坛(PDC)上登台亮相。C#是微软公司研究员Anders Hejlsberg的最新成果。C#看起来与Java有着惊人的相似;它包括了诸如单一继承、接口、与Java几乎同样的语法和编译成中间代码再运行的过程。但是C#与Java有着明显的不同,它借鉴了Delphi的一个特点,与COM(_c#读文件
文章浏览阅读4.8w次,点赞12次,收藏44次。C#创建Excel文件,这里实际上是从资源中提取一个事先创建好的Excel文件,文件提取成功后,使用OleDb方法连接Excel,向Excel文件中写入数据。创建解决方案菜单》新建》项目》Windows窗体应用程序:添加相关组件:添加两个DataGridView,一个TextBox,两个按钮 ,如下图:添加Excel资源:先在文件夹中新建一个Excel文件,在Sheet1表的第一行设置列名:双击“_c#保存到excel
文章浏览阅读2.8k次。windows 7和vista提高的系统的安全性,同时需要明确指定“以管理员身份运行”才可赋予被运行软件比较高级的权限,比如访问注册表等。否则,当以普通身份运行的程序需要访问较高级的系统资源时,将会抛出异常。如何让程序在启动时,自动要求“管理员”权限了,我们只需要修改app.manifest文件中的配置项即可。app.manifest文件默认是不存在的,我们可以通过以下操作来自_vb.net 程式以管理员运行
文章浏览阅读6.1k次,点赞4次,收藏7次。窗口风格(Window style)WS_BORDER 有边框窗口 WS_CAPTION 必须和WS_BORDER风格配合,但不能与WS_DLGFRAME风格一起使用。指示窗口包含标题要部分。 WS_CHILD 说明窗口为子窗口,不能应用于弹出式窗口风格(WS_POPUP)。 WS_CHILDWINDOW 同WS_CHILD。 WS_CLIPCHILDREN 绘制父窗口时_net(c#):ws_caption | ws_border
文章浏览阅读1.8w次,点赞3次,收藏9次。C#修改文件或文件夹的权限,为指定用户、用户组添加完全控制权限 //给Excel文件添加"Everyone,Users"用户组的完全控制权限 FileInfo fi = new FileInfo(excelPath); System.Security.AccessControl.FileSecurity fileSecurity_c# 判断 文件夹 是否 users 用户组 写入 权限
文章浏览阅读9.3k次。C# 模拟PrintScreen 和 Alt+PrintScreen截取屏幕图片keybd_event API函数功能:该函数合成一次击键事件。系统可使用这种合成的击键事件来产生WM_KEYUP或WM_KEYDOWN消息,键盘驱动程序的中断处理程序调用keybd_event函数。在Windows NT中该函数己被使用SendInput来替代它。函数原型;VOID keybd_event..._如何编程调用 printscreen
文章浏览阅读1w次。这本来是在VS2005下创建的一下项目,后来改用VS2010的开发环境,.NET Framework的版本还是使用2.0,但每次生成之后都会在解决方案的同级目录下产生一个名称乱码的文件夹,解决了那个问题之后,由于这个Windows窗体应用程序添加一个安装项目,项目生成时出现以下错误:错误 1 验证时出错。HRESULT = '8000000A' F:CsProjects屏幕截图2005屏幕截_error1an error occurred while validating. hresult = '8000000a
文章浏览阅读7.3k次。上一篇:C#软件开发实例.私人订制自己的屏幕截图工具(六)添加配置管理功能由于截图时可能需要精确截取某一部分,所以需要放大镜的功能,这样截取的时候才更容易定位截图的位置。添加PictureBox,name属性设置为“pictureBox_zoom”;在“Form1_Load”事件处理函数中添加以下代码://设置放大镜的大小 this.pictureBox_zoom.Widt_c#实现放大镜效果
文章浏览阅读4.5k次。C# 绘制箭头的方法,仿微信截图的箭头效果见下图,实际上还是有区别的,箭头的起点处微信的是圆端,而我实现的是尖端。说说我的实现吧,实现方法其实是划线,线的两端都要设置端点样式。看代码:Point _StarPoint = new Point(0, 0);Point _EndPoint = new Point(300, 300);System.Drawing.Drawing2..._adjustablearrowcap
文章浏览阅读1.3w次,点赞3次,收藏4次。在实现“C#软件开发实例.私人订制自己的屏幕截图工具(六)添加配置管理功能”功能时,遇到警告:由于“Screenshot.Form1.ZoomBoxHeight”是引用封送类的字段,访问上面的成员可能导致运行时异常解决方案:对字段对待封装:在需要封装的字段上单击鼠标右键,重构》封装字段:输入属性名:使用默认设置,单击应用_由于引入封装类的字段