Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 临时存储文件_C# - Fatal编程技术网

C# 临时存储文件

C# 临时存储文件,c#,C#,我有这段代码用于图像的拖放功能。它的问题是,它将文件暂时保存到我的计算机@“C:\Users\xxxx\Desktop\。这是一个问题,因为这意味着用户无法使用此功能。因此,我如何调整代码以使其在线工作 我需要找到一种临时存储图像的方法 public ActionResult SaveUploadedFile(string test) { foreach (string fileName in Request.Files) {

我有这段代码用于图像的拖放功能。它的问题是,它将文件暂时保存到我的计算机
@“C:\Users\xxxx\Desktop\
。这是一个问题,因为这意味着用户无法使用此功能。因此,我如何调整代码以使其在线工作

我需要找到一种临时存储图像的方法

public ActionResult SaveUploadedFile(string test)
        {


            foreach (string fileName in Request.Files)
            {
                HttpPostedFileBase file = Request.Files[fileName];


                var filename = Path.GetFileName(file.FileName);

                var sourceimage = System.Drawing.Image.FromStream(file.InputStream);
                img = sourceimage;

                var dog = new Dog();
                byte[] tempImg = dog.imageToByteArray(sourceimage);

                var fullPath = @"C:\Users\xxxx\Desktop\" + filename;
                file.SaveAs(fullPath);



                string link = dog.UploadImage(fullPath);

                JObject o = JObject.Parse(link);

                string name = (string) o.SelectToken("data.link");

                System.IO.File.Delete(@"C:\Users\xxxx\Desktop\" + filename);
                var page = RavenSession.Load<DogContentPage>(test);
                page.Image = name;
                RavenSession.SaveChanges();



            }
我认为有两种解决方案:

N°1:服务器中的一项任务,如果必须删除任何文件,它将进行检查,比如说每天检查两次

N°2:应用程序调用的一个过程,用于验证现在与文件过期日期之间的差异

希望这些想法能有所帮助。

2解决方案我认为:

N°1:服务器中的一项任务,如果必须删除任何文件,它将进行检查,比如说每天检查两次

N°2:应用程序调用的一个过程,用于验证现在与文件过期日期之间的差异


希望这些想法能有所帮助。

与使用硬编码字符串不同,您应该使用以下方法查找用于存储临时文件的临时文件夹:

string tempPath = System.IO.Path.GetTempPath();

您不应使用硬编码字符串,而应使用以下内容查找要在其中存储临时文件的临时文件夹:

string tempPath = System.IO.Path.GetTempPath();

请查看有助于您的
Path.GetTempPath()
以及
Path.GetTempFileName()

一个快速的解决办法是:

 // var fullPath = @"C:\Users\xxxx\Desktop\" + filename; // Not good, since user folder
 var fullPath = Path.GetTempFileName(); // Temp file, with unqiue name
我建议使用
Path.GetTempFileName()
而不是
Path.GetTempPath()+filename
的原因是,当您只上载文件字节时,文件名并不重要,而且它保证了文件名的唯一性,因此不可能出现文件名冲突

var fullPath = Path.GetTempPath() + filename; // No good, could clash if the filenames were the same

请查看有助于您的
Path.GetTempPath()
以及
Path.GetTempFileName()

一个快速的解决办法是:

 // var fullPath = @"C:\Users\xxxx\Desktop\" + filename; // Not good, since user folder
 var fullPath = Path.GetTempFileName(); // Temp file, with unqiue name
我建议使用
Path.GetTempFileName()
而不是
Path.GetTempPath()+filename
的原因是,当您只上载文件字节时,文件名并不重要,而且它保证了文件名的唯一性,因此不可能出现文件名冲突

var fullPath = Path.GetTempPath() + filename; // No good, could clash if the filenames were the same

您应该使用users temp文件夹。因此,不要使用
@“C:\users\xxxx\Desktop\”
而是使用
System.Environment.GetEnvironmentVariable(“temp”)
System.IO.Path.GetTempPath()
…永远不要硬编码文件路径。您应该使用users temp文件夹,而不是
@“C:\users\xxxx\Desktop”\“
使用
System.Environment.GetEnvironmentVariable(“TEMP”)
System.IO.Path.GetTempPath()
。。。永远不要硬编码文件路径。非常感谢!我要试试这个@用户2915962很高兴我能帮忙:)非常感谢!我要试试这个@用户2915962很高兴我能帮忙:)