C#ASP.NET文件上载问题 [英] C# ASP.NET File Upload issue

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

问题描述

以下是将图像上传到FTP的C#代码。我的问题是:



1.我希望能够在filename变量中获取完整路径以获取正确的文件或任何其他选项来解决它。



2.如何避免上传时出现错误:请求获得类型

Below is my C# code to upload images in to the FTP. My problems is:

1. I want to able to get full path in filename variable to get correct file or any other option to solve it.

2. How to avoid following error while uploading: Request for the permission of type

'System.Net.WebPermission, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. 





尊敬的各位专家,请给我一个简单的建议,将图像上传到ftp,因为我在2-3天左右搜索这个我真的很失望。谢谢!



Dear Experts, please give me simple advise to upload a images to ftp, because I'm searching this around 2-3 days and I'm really disappointed. Thanks!

protected void UploadButton_Click(object sender, EventArgs e)
{
 if (FileUploadControl.HasFile)
            {
                try
                {
                    if (FileUploadControl.PostedFile.ContentType == "image/jpeg")
                    {
                        if (FileUploadControl.PostedFile.ContentLength < 5242881)
                        {
                            string filename = Path.GetFileName(FileUploadControl.FileName);
                                                    
                            Stream image;
                                string target = "1.jpg";

                                FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://ftppath" + target);
                                req.UseBinary = true;
                                req.Method = WebRequestMethods.Ftp.UploadFile;
                                req.Credentials = new NetworkCredential("ftpUname", "ftpPass");

                                byte[] fileData = File.ReadAllBytes(filename);

                                req.ContentLength = fileData.Length;
                                Stream reqStream = req.GetRequestStream();
                                reqStream.Write(fileData, 0, fileData.Length);
                                reqStream.Close();                            
                             
                             StatusLabel.Text = "Upload status: File uploaded!";
                        }
                        else
                            StatusLabel.Text = "Upload status: The file has to be less than 100 kb!";
                    }
                    else
                        StatusLabel.Text = "Upload status: Only JPEG files are accepted!";
                }
                catch (Exception ex)
                {
                    StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
                }
}

推荐答案

使用以下功能可以访问文件的完整路径

Full path of the file can be access by using below function
string filepath = Path.getfilename (fileupload1.filename);



rgarding权限问题:

您需要为修改权限的该文件夹上的所有人组设置权限。不提供完全访问权限,但需要修改才能解决权限问题。


rgarding permission issue :
you need to set permission for everyone group on that folder with modify right. don't give full access but modify is needed to resolve the permission issue.


这篇关于C#ASP.NET文件上载问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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