如何在ASP.NET C#中上传带附件的数据 [英] How to upload data with attachments in ASP.NET C#

查看:86
本文介绍了如何在ASP.NET C#中上传带附件的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,



我有一个网络应用程序。要求是使用带有附件的excel表上传数据。



我尝试过:



我已经从excel表中完成了上传数据,但是我无法上传附件文件。

示例数据:
- -------------------------------------------------- ------------------------------
| CandidateID | CandidateName |年龄|图片| IsActive |
---------------------------------------------- -----------------------------------
| 01 |约翰| 22 | C:\ User \Abc \ MyFiles; \\ J123.jpg |是的|
---------------------------------------------- ----------------------------------



*候选图片路径来自客户端的机器。



我如何允许客户端上传带有附件的数据(Excel表格)?



任何人都可以帮助我,我怎样才能上传带有asp.net C#数据的图片文件。





提前致谢

解决方案

您可以将图像转换为base64并将其保存为字符串。在数据表中保存nvarchar(max)字段。 />


1.将图像转换为base64:



 string path =D:\\ \\\SampleImage.jpg; 
using(System.Drawing.Image image = System.Drawing.Image.FromFile(path))
{
using(MemoryStream m = new MemoryStream())
{
image.Save(m,image.RawFormat);
byte [] imageBytes = m.ToArray();
base64String = Convert.ToBase64String(imageBytes);
返回base64String;
}
}





2.将base64字符串转换为图片:

< br $>


 public System.Drawing.Image Base64ToImage(string base64String)
{
byte [] imageBytes = Convert.FromBase64String( base64String);
MemoryStream ms = new MemoryStream(imageBytes,0,imageBytes.Length);
ms.Write(imageBytes,0,imageBytes.Length);
System.Drawing.Image image = System.Drawing.Image.FromStream(ms,true);
返回图片;
}


1。您将需要一个HTML表单,其属性设置为附加文件:

< form action =upload.aspxmethod =postenctype =multipart / form-data>



2.所述表格需要有一个 input 元素用于文件:

< input type =filename =fileToUploadid =fileToUpload>



3.您需要有一个服务器端脚本来处理表单并保存文件。

我没有做所有的工作,试试谷歌;我做了

ASP.NET - 文件上传 [ ^ ]

Dear All,

I have a web application. The requirement is to upload the data using excel sheet with attachments.

What I have tried:

I have already completed upload data from excel sheet but I'm not able to upload the attachments files.

Example data:
---------------------------------------------------------------------------------
|CandidateID | CandidateName| Age | Picture                        | IsActive   |
---------------------------------------------------------------------------------
| 01         |   John       | 22  | C:\User\Abc\MyFiles\J123.jpg   |   Yes      |
--------------------------------------------------------------------------------


*The candidate picture path is from the client's machine.

How can I allow the client to upload data(Excel sheet) with attachment?

Can anyone please help me, how can I upload the picture file with data in asp.net C#.


Thanks in advance

解决方案

You can convert the image to base64 and save it as a string.Keep the field of nvarchar(max) in datatable.

1. Convert image to base64:

string path = "D:\\SampleImage.jpg";  
    using(System.Drawing.Image image = System.Drawing.Image.FromFile(path))  
    {  
        using(MemoryStream m = new MemoryStream())  
        {  
            image.Save(m, image.RawFormat);  
            byte[] imageBytes = m.ToArray();  
            base64String = Convert.ToBase64String(imageBytes);  
            return base64String;  
        }  
    }  



2. Convert base64 string to image:


public System.Drawing.Image Base64ToImage(string base64String)   
{  
    byte[] imageBytes = Convert.FromBase64String(base64String);  
    MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);  
    ms.Write(imageBytes, 0, imageBytes.Length);  
    System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);  
    return image;  
}  


1. You will need an HTML form with its attributes set to have a file attached:
<form action="upload.aspx" method="post" enctype="multipart/form-data">

2. Said form will need to have an input element for a file:
<input type="file" name="fileToUpload" id="fileToUpload">

3. You will need to have a server side script to process the form and save the file.
I'm not doing all the work, try Google; I did
ASP.NET - File Uploading[^]


这篇关于如何在ASP.NET C#中上传带附件的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆