File not found exception once deployed to Server

情到浓时终转凉″ 提交于 2019-12-23 06:01:34

问题


I am using the below code to Upload an Image file to a SharePoint Document Library. The code works fine locally but once deployed to server, i get the Exception as file not found.

                String fileToUpload = FlUpldImage.PostedFile.FileName; //@"C:\Users\admin.RSS\Desktop\Photos\me_skype.jpg";
                String documentLibraryName = "SiteAssets";
                if (!System.IO.File.Exists(fileToUpload))
                    throw new FileNotFoundException("File not found.", fileToUpload);

                SPFolder myLibrary = web.Folders[documentLibraryName];

                // Prepare to upload
                Boolean replaceExistingFiles = true;
                String fileName = CheckStringNull(txtFirstName.Text) + CheckStringNull(txtLastName.Text) + CheckDateNull(txtDOB) + System.IO.Path.GetFileName(fileToUpload); ;
                if (fileName.Contains('/'))
                {
                    fileName = fileName.Replace("/", "");
                }
                if (fileName.Contains(':'))
                {
                    fileName = fileName.Replace(":", "");
                }
                FileStream fileStream = File.OpenRead(fileToUpload);
                //Upload document
                SPFile spfile = myLibrary.Files.Add(fileName, fileStream, replaceExistingFiles);
                string url = site.ToString() + "/" + spfile.ToString();
                if (url.Contains("="))
                {
                    url = url.Split('=')[1];
                }
                //Commit
                myLibrary.Update();

The string fileupload contains URL as C:\Users\admin.RSS\Desktop\Photos\me.jpg This URL is actually the client system and the server side code throws exception as file not found. How to handle this issue?

UPDATE:

I removed the lines of code that checks if the file exists and now i get the exeption on FileStream fileStream = File.OpenRead(fileToUpload); as c:\windows\system32\inetsrv\20120605_133145.jpg cold not be found

Kindly help. Thank You


回答1:


        if (this.fuAvatarUpload.HasFile && this.fuAvatarUpload.PostedFile.FileName.Length > 0)
        {
            string extension = Path.GetExtension(file.FileName).ToLower();
            string mimetype;
            switch (extension)
            {
                case ".png":
                case ".jpg":
                case ".gif":
                    mimetype = file.ContentType;
                    break;

                default:
                    _model.ShowMessage("We only accept .png, .jpg, and .gif!");
                    return;
            }

            if (file.ContentLength / 1000 < 1000)
            {
                Image image = Image.FromStream(file.InputStream);
                Bitmap resized = new Bitmap(image, 150, 150);
                byte[] byteArr = new byte[file.InputStream.Length];
                using (MemoryStream stream = new MemoryStream())
                {
                    resized.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
                    byteArr = stream.ToArray();
                }
                file.InputStream.Read(byteArr, 0, byteArr.Length);
                profile.ImageUrl = byteArr;
                profile.UseGravatar = false;
                profileService.UpdateProfile(profile);
                this._model.ShowApprovePanel();
            }
            else
            {
                _model.ShowMessage("The file you uploaded is larger than the 1mb limit.  Please reduce the size of your file and try again.");
            }
        }



回答2:


Saving the file physically onto server and than working on the same helped me resolve my issue.



来源:https://stackoverflow.com/questions/11877829/file-not-found-exception-once-deployed-to-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!