我在文件上载saveas命令时遇到问题 [英] Im having trouble with a file upload saveas command

查看:126
本文介绍了我在文件上载saveas命令时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的aspx页面,可以上传照片,当我通过vis studio测试页面时,一切正常,但是,当我将页面/代码上传到我的虚拟主机(hostgator Windows共享包)时,文件上传saveas出现错误一行,错误不是很具体,只是告诉我它是一个运行时错误,代码在下面,有人可以阐明为什么这是从我的主机服务器而不是本地计算机上的vis Studio发生的吗?

I have a simple aspx page which allows photo uploads, when I test the page through vis studio everything works fine however when I upload the page/code to my webhost (hostgator windows shared package) I am getting errors at the fileupload saveas line, the errors arent very specific, just telling me its a runtime error, code is below, can anyone shed some light on why this is happening from my host server but not with vis studio on my local machine?

protected void btnUploadPhoto_Click(object sender, EventArgs e)
        {
            string imageFolder = "Media";
            string savePath;
            string saveFile;

            if (fileUploadPhoto.HasFile)
            {
                savePath = Path.Combine(Request.PhysicalApplicationPath, imageFolder);
                saveFile = Path.Combine(savePath, fileUploadPhoto.FileName);
                //lblConfirmPhotoUpload.Text = saveFile; // test line only
                fileUploadPhoto.SaveAs(saveFile);

                SqlDataSourcePhotos.Insert();
                gridViewPhotos.DataBind();
            }
        }

推荐答案

好吧,我相信在生产环境中,您需要使用Server.MapPath()作为路径.这样可以解决您的问题.因此,在合并它们之后,尝试将其路径添加到Server.MapPath()中.对于MSDN链接,请此处 [
Well, I believe in production you need to use Server.MapPath() for your path. This will solve your problem. So try adding your path into Server.MapPath() after combining them. For MSDN link look here[^].

[UPDATE]After OP''s comment[/UPDATE]

it should be something like:
string path = Server.MapPath(saveFile);
if(File.Exists(path)) // This is just to make sure if the file exists or not
{
  fileUploadPhoto.SaveAs(path);
}



祝你好运,
OI



Good luck,
OI


首先,通过简单地将方法主体封装在try-catch块中,并在出现错误的情况下打印异常,通常可以使您查明错误的位置.

其次,按照Orcun的指示检查代码后,请阅读
服务器. MapPath [^ ]方法.它允许您将〜/images/"之类的路径映射到物理磁盘路径.

另外,我最好的猜测是这是文件权限问题.运行Web服务器所使用的帐户可能没有权限在您的计算机上创建某些位置的文件.检查一下.

希望这对您有帮助
First of all, by simply encapsulating the method body in a try-catch block and printing the exception in case of an error should usually let you pin-point the location of your error.

Secondly, after going through your code, as Orcun directed you, please read upon Server.MapPath[^] method. It allows you to map a path like "~/images/" to physical disk paths.

Additionally, my best guess is that this is a file permission issue. The account under which your web server is run as, might not have permissions on your machine to create files on some locations. Check on that.

Hope this helps


使用Server.MapPath将文件保存在服务器上可能在服务器上您无权访问特定文件夹:
use Server.MapPath to save file on server It may be possible on server you don''t have access to specific folder:
fileUploadPhoto.SaveAs(Server.MapPath("Images/..."));


这将为您提供帮助.


This will help you.


这篇关于我在文件上载saveas命令时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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