文件上传在localhost上工作但在IIS上发布后无法远程工作? [英] File upload working on localhost but not working remotly after publishing on IIS?

查看:78
本文介绍了文件上传在localhost上工作但在IIS上发布后无法远程工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iis上找不到文件但在localhost上传。



错误:



File not found on iis but uploading in localhost.

Error:

System.IO.FileNotFoundException: Could not find file 'C:\Users\...









System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access) at WebPages_frmAddSoftware.ConvertClientImageTo_base64String(HttpPostedFile file) at WebPages_frmAddSoftware.btnUpload_Click(Object sender, EventArgs e) 





我尝试过:





What I have tried:

protected void btnUpload_Click(object sender, EventArgs e)
   {
       try
       {

           if (btnFileUpload.HasFile == true  )
           {

               List<string> extentions = new List<string> { ".jpg", ".gif", ".png" };

               if (extentions.Contains(  Path.GetExtension(btnFileUpload.FileName).ToLower()))
               {
                   lblError.Visible = false;

                   // Get Client side full Image path need to be convert into HttpPostedFile
                   HttpPostedFile file = (HttpPostedFile)btnFileUpload.PostedFile;

                   //get file extention

                   image_base64String = ConvertClientImageTo_base64String(file);

                   imgSoftLogo.Src = @"data:image/gif;base64," + image_base64String;

                   Session["image"] = image_base64String;

               }


               else
               {

                   lblError.ForeColor = System.Drawing.Color.Red;
                   lblError.Text = "Please Upload Valid File.";
               }


           }

       }
       catch (Exception ex) { lblPublish0.Text= ex.ToString(); }
   }










public string ConvertClientImageTo_base64String(HttpPostedFile file)
   {
       byte[] byteArray = null;

       file = (HttpPostedFile)btnFileUpload.PostedFile;

       //Use FileStream to convert the image into byte.
       using (FileStream fs = new FileStream(file.FileName, FileMode.Open, FileAccess.Read))
       {
           byteArray = new byte[fs.Length];
           int iBytesRead = fs.Read(byteArray, 0, (int)fs.Length);
           if (byteArray != null && byteArray.Length > 0)
           {
               // Convert the byte into image
               image_base64String = Convert.ToBase64String(byteArray, 0, byteArray.Length);
           }
           return image_base64String;
       }
   }

推荐答案

file.FileName





这只是简单的myimage.jpg,所以当你只是保存到myimage.jpg时,你认为该文件会在哪里发生? ?它会进入什么文件夹?看起来它默认为该帐户运行的用户的文件夹,毫无疑问存在安全问题。您必须为图像提供完整的显式路径,并将其保存在您具有写入权限的Web空间中的某个位置,例如app_data文件夹





This is going to be simply "myimage.jpg", so where do you think the file will go when you simply save to "myimage.jpg"? What folder will it go in? It looks like it is defaulting to the folder for the user the account is running under and there are no doubt security problems there. You have to give a full explicit path for the image and save it somewhere in your web space that you have write access to like the app_data folder

string filename = Server.MapPath(Path.Combine("~/App_Data", file.Filename));





或者如果你想要直接为图像服务,你可能想把它们放在你创建的特定文件夹中





Or if you want to server the images directly you might want to put them in a specific folder you've created

string filename = Server.MapPath(Path.Combine("~/uploads", file.Filename));


根据浏览器的不同, FileName property返回文件名或用户计算机上文件的路径



您的代码正在运行在服务器上。即使您在用户的系统上获得文件的完整路径,它几乎肯定不存在于服务器上。即使它确实如此,它也不会包含相同的内容。



在Visual中调试代码时,它可能出现 Studio,或在您的计算机上。但这只是因为,在特定情况下,服务器和客户端是相同的。



您需要将文件保存在服务器上的某个位置,或者使用 InputStream 属性,用于读取文件内容。

Depending on the browser, the FileName property returns either the name of the file, or the path of the file on the user's computer.

Your code is running on the server. Even if you get the full path of the file on the user's system, it almost certainly doesn't exist on the server. And even if it did, it won't contain the same content.

It might appear to work when you debug your code in Visual Studio, or on your computer. But that's only because, in that specific instance, the server and client are the same.

You either need to save the file somewhere on the server, or use the InputStream property to read the contents of the file.
public string ConvertClientImageTo_base64String(HttpPostedFile file)
{
    int length = (int)file.InputStream.Length;
    if (length == 0) return string.Empty;
    
    byte[] byteArray = new byte[length];
    file.InputStream.Read(byteArray, 0, length);
    return Convert.ToBase64String(byteArray);
}




private static readonly ICollection<string> AllowedExtensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
    ".jpg", ".gif", ".png"
};

protected void btnUpload_Click(object sender, EventArgs e)
{
    try
    {
        if (btnFileUpload.HasFile)
        {
            if (AllowedExtentions.Contains(Path.GetExtension(btnFileUpload.FileName)))
            {
                HttpPostedFile file = btnFileUpload.PostedFile;
                string base64String = ConvertClientImageTo_base64String(file);
                Session["image"] = base64String;
                
                // TODO: Specify the correct MIME type for the image:
                imgSoftLogo.Src = @"data:image/gif;base64," + base64String;
                
                lblError.Visible = false;
            }
            else
            {
                lblError.ForeColor = System.Drawing.Color.Red;
                lblError.Text = "Please Upload Valid File.";
            }
        }
    }
    catch (Exception ex) 
    {
        lblPublish0.Text= ex.ToString(); 
    }
}


这篇关于文件上传在localhost上工作但在IIS上发布后无法远程工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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