前言
Unity 基于Paroxe PDFRenderer 实现PC/WebGL 平台PDF 读取显示及下载_2301_79311694的博客-CSDN博客首先导入Paroxe PDFRenderer 插件。https://blog.csdn.net/2301_79311694/article/details/132870238这篇关于PDF 的下载中,PC 平台下载使用的保存路径是写在代码里,要修改保存路径就要修改代码,增加一个Windows 窗口选取路径就可以相对自由地选择保存路径
参考
实现
FileDlog类
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class FileDlog
{
public int structSize = 0;
public IntPtr dlgOwner = IntPtr.Zero;
public IntPtr instance = IntPtr.Zero;
public String filter = null;
public String customFilter = null;
public int maxCustFilter = 0;
public int filterIndex = 0;
public String file = null;
public int maxFile = 0;
public String fileTitle = null;
public int maxFileTitle = 0;
public String initialDir = null;
public String title = null;
public int flags = 0;
public short fileOffset = 0;
public short fileExtension = 0;
public String defExt = null;
public IntPtr custData = IntPtr.Zero;
public IntPtr hook = IntPtr.Zero;
public String templateName = null;
public IntPtr reservedPtr = IntPtr.Zero;
public int reservedInt = 0;
public int flagsEx = 0;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class OpenFileDlg : FileDlog
{
}
public class OpenFileDialog
{
[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
public static extern bool GetSaveFileName([In, Out] OpenFileDlg ofd);
}
打开保存文件窗口
OpenFileDlg sfd = new OpenFileDlg();
sfd.structSize = Marshal.SizeOf(sfd);
sfd.filter = "PDF文件(*.pdf)\0*.pdf";
sfd.file = new string(new char[256]);
sfd.maxFile = sfd.file.Length;
sfd.file = downloadFileName; //默认文件名称
sfd.fileTitle = new string(new char[64]);
sfd.maxFileTitle = sfd.fileTitle.Length;
sfd.initialDir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); //默认路径
sfd.title = "保存文件"; //窗口标题
sfd.defExt = "pdf";
sfd.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
if (OpenFileDialog.GetSaveFileName(sfd))
{
sfd.file = sfd.file.Replace('\\', '/');
downloadPath = sfd.file;
UnityWebRequest webRequest = UnityWebRequest.Get(url);
yield return webRequest.SendWebRequest();
if (!webRequest.isNetworkError && webRequest.isDone)
{
var file = webRequest.downloadHandler.data;
FileStream fileStream = new FileStream(downloadPath, FileMode.Create);
fileStream.Write(file, 0, file.Length);
fileStream.Close();
}
}
之前的downloadPath 只到文件夹为止,所以需要加上downloadFileName,sfd.file 已经是包括后缀名在内的完整路径,就无需再组合文件夹路径和文件名称